c++ - Linkedlist - Templates - Pointer -
what wrong code? i'm getting error @ runner = runner->next;
, after debugging, don't see what's wrong it.
void linkedlist<t>::add(t item) { if (root == 0) { root = new node<t>(item); cout << "add, root empty \n"; } else { cout << "add, root not empty \n"; node<t> * runner = root; while (runner != 0) { runner = runner->next; } runner = new node<t>(item); } }
it should this:
node<t> * runner = root; while (runner->next != null) { runner = runner->next; } runner->next = new node<t>(item);
your code changes local variable, , not actual node. (note runner->next
object in actual list, while runner
local variable.)
Comments
Post a Comment