Hi all, I tried to link a new node(NewNode) at the end of the list but it is not executing the last statement (last->next = NewNode). Could you guys help me to solve this problem? Thank you in advance.
this is subtle. You scroll through the list until last DOES equal NULL. (modern c++ uses nullptr instead of NULL). When it DOES equal NULL, you assign it. That is NOT part of the list, its just a standard NULL pointer!!
what you have to do is like this:
if(last) //same as last != null while(last->next != null)
last = last->next;
last = newnode; //if last was null, it goes here. right?
it is also highly useful to KEEP last as a class member that is always in the correct place so you do not have to loop through the whole list every time you need to add one. Then your insert and delete (and anything else that does this kind of operation) maintain last and use it.
Correction: last is null so last->next is a crash/undefined behavior @ line 15. Regardless, the fix is still to use next off the last one.