It might simplify the program if you write two more functions: 1) a function that inserts a new node into the list in its correct position, and 2) a function that displays all the nodes in the list. Then main() can contain a simple loop
void InsertNode(node**head, node*p)
{
}
void DisplayList(node* head)
{
}
int main()
{
node* head = NULL; // top of linked list
node* p = NULL; // a new node
while(true) // an infinite loop
{
p = new node;
cout << "Give a number: ";
cin >> p->data;
p->next = NULL;
InsertNode(&head, p);
DisplayList(head);
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Hey thanks! :) but how do I insert the node into the right position? Does that mean I have to compare the input values so they would be arranged in an ascending order? That would mean i would use condition statement?
you are correct. You will have to use a loop to iterate through the linked list to find the correct spot for insertion.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
That will work too, as long as you swap only the data values instead of the whole node. The next pointers has to be kept unchanged.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343