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;
}