havn't programmed in c++ in well over a year and taking a new class at a different college. So I completly forgot how to work with linked lists. we are doing review so notes in book/slides don't include too munch information and is all in pseudo code. at the moment I am trying to make a single linked list. 90% sure the problem is in the main sending the head. if you can explain what to do in detail I would greatly appreciate it!

#include <iostream>
using namespace std;

struct node
{
	int  Item;
	node* Next;
}; // end struct

node* head;
typedef node* ptrType;
//====================================================================
void insert_node(ptrType& Head, int value)
{

	ptrType Cur;
	ptrType Prev;
	ptrType NewPtr;
	bool inserted;
			
	NewPtr = new node;
	NewPtr -> Item = value;
	NewPtr -> Next = NULL;  // just to be on the safe side
		                           

	if (Head == NULL) 
		Head = NewPtr; // case 1
	// NewPtr -> Next remains NULL, since
	// this is the list's end
	else if (NewPtr -> Item <= Head -> Item)
	{   // case 2
		NewPtr -> Next = Head;
		Head = NewPtr;
	}
	else
	{ 
		Prev = Head;
		Cur = Head -> Next;
		inserted = false;

		while (Cur != NULL && !inserted)
		{
			if (NewPtr -> Item <= Cur -> Item)
			{  // case 3
				NewPtr -> Next = Cur;
				Prev -> Next = NewPtr;
				inserted = true;
				cout << Cur << endl;
			}
			else
			{
				Prev = Prev -> Next;
				Cur  = Cur -> Next;
			}
		}
		if (!inserted)
			Prev -> Next = NewPtr; // case 4
		// NewPtr -> Next remains NULL,
		// since this is the list's end
	}
}
//==================================================================
void print_circle(ptrType Head)
{
	ptrType current;
	ptrType next;
	if(head == NULL)
		cout << "empty list\n";
	else if(head != NULL)
	{
		while(current != NULL)
		{
			cout << current->Item << endl;
			current = current->Next;
		}
	}
}
//==================================================================

int main()
{
	for (int n = 1;n<10;n++)
		insert_node(head,n);
//	cout << &head;
//	print_circle(head);

	return 0;
}

Edited 5 Years Ago by jigglymig: n/a

site looks decent. I like the how descriptive the concept is... but the code on implementation seems to be completly different than what I am trying to do, or am I just trying to do it all wrong? the code is very hard for me to understand, because the words/abreviations the person used(rv, jsw_node...ect). I just need help making my code work, and to understand what I am doing wrong.

Edited 5 Years Ago by jigglymig: n/a

Here is also a very basic, simple and short insert function, yours looks rather cluttered

void insertAsFirstElement(nodeType *&first, nodeType *&current, int num)
{
	nodeType *tmp = new nodeType;
	tmp->num = num;
	tmp->link = NULL;
	first = tmp;
	current = tmp;
}

void insert(nodeType *&first, nodeType *&current, int num)
{
	if (isEmpty(first))
		insertAsFirstElement(first, current, num);
	else
	{
		nodeType *tmp = new nodeType;
		tmp->num = num;
		tmp->link = NULL;
		current->link = tmp;
		current = tmp;
	}
}

^ Head is not initialized in the code. I think i am not sending head correctly. won't display data i sent in. so I think I am not calling insert correctly, or I am not calling display correctly. when i have it call display it errors up saying there is no data or something like that

I have to go to class. I'll impliment that in my code and see if it fixes it... thanks for the help thus far! I appreciate it.

This question has already been answered. Start a new discussion instead.