I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.
while(current)
{
if (current->next == NULL)
{
node *newNode = new node;
current->next = newNode;
newNode->data = newdata;
newNode->next = NULL;
}
current = current->next; // current now points to new node.
}
}
In this loop current shall never be NULL because you always assign it to point to new Node. instead, you should just break out of the loop after adding.
Or I would do it as follows:
1 2 3 4 5 6 7 8 9
while (current->next) // move to the last node
current = current->next;
// Now simply add the new node.
node *newNode = new node;
current->next = newNode;
newNode->data = newdata;
newNode->next = NULL;
current = current->next;