I am just learning linked lists and I need to start off my inputing integers and outputing them in a sorted list. I have written a code and pretty confident that the code make sense, but maybe it is out of sequence is why it is not working? I am able to input the integers, which means the loop is working, but then it stops. Code is below. Do I maybe need to put the Sorting information also into a function? And if so, I guess I am confused as to where in Int Main I would put it -- in the loop? Any help is appreciated.
I pasted the code below, but lost its format so I tried to reformat as best I could. THANKS!!
**********************************************************
#include <iostream>
#include <fstream>
using namespace std;
//Functions
void GetData (int& number);
const int nil=0;
class node_type
{
public:
int num; // Numbers that we are entering
node_type *next;
};
void main ()
{
node_type *first, *p, *q, *newnode;
int i;
int number;
float sum = 0;
first = new node_type;
p=first;
GetData(number);
(*first).num = number;
(*first).next = nil;
for (i=2; i<=13; i++)
{
GetData (number);
newnode = new node_type;
(*newnode).num=number;
(*newnode).next=nil;
(*p).next = newnode;
p=newnode;
}
//Now Sort the List
if (first == nil)
{
first = newnode;
}
else
{
if ((*newnode).num <=(*first).num)
{
(*newnode).next = first;
first = newnode;
}
else
q=first;
p=(*q).next;
if (p==nil)
(*first).next=newnode;
else
while ((*p).num < (*newnode).num && ((*p).next!=nil))
q=p;
p=(*p).next;
//endwhile
//endif
if ((*p).num >= (*newnode).num)
{
(*q).next=newnode;
(*newnode).next = p;
}
else
(*p).next=newnode;
//endif
}//endif
//endif
q=first;
cout<< "The list contains \n";
while (q != nil)
{
cout<<" The numbers are: " << (*q).num << "\n";
q = (*q).next;
}
//endwhile
}
void GetData(int& number)
{
cout<< "Enter an integer \n";
cin>>number;
cout<< number << "\n";
}