Reputation: 965
What is wrong this code? I'm getting an error at runner = runner->next;
, after debugging, but I don't see what's wrong with it.
void Linkedlist<T>::add(T item)
{
if (root == 0)
{
root = new Node<T>(item);
cout << "Add, Root is empty \n";
}
else
{
cout << "Add, Root is not empty \n";
Node<T> * runner = root;
while (runner != 0)
{
runner = runner->next;
}
runner = new Node<T>(item);
}
}
Upvotes: 0
Views: 62
Reputation: 476970
It should be like this:
Node<T> * runner = root;
while (runner->next != NULL)
{
runner = runner->next;
}
runner->next = new Node<T>(item);
Your code only changes a local variable, and not the actual node. (Note that runner->next
is an object in your actual list, while runner
is just a local variable.)
Upvotes: 2