Each node in the list holds a single piece of data along with a link to the next node in the chain.
To add a new item you need to create a new node, initialize it's data and then set the final node in the current list to link to the new node.
This code creates a new node (value of 5 and null (0) for next) and then finds the end of the list before placing it there.
Head is the first node in the list which seems a bit different from what you've used above.
You can also set the new node as Tail each time to save you having to traverse the list to find the end each time.
CODE
//Creating the new node
node *temp = new node;
temp->data = 5;
temp->next = 0;
//a Temp node used for traversing the list
node *current;
current = head;
//Finding the end of the list.
while (current->next != 0){
current = current->next;
}
//Attaching the new node to the end.
current->next = temp;
This post has been edited by Mallstrop: 9 Jul, 2008 - 03:34 AM