I know there are other approaches to delete nodes, but I couldn't figure out why mine is wrong.
I want to know at what step I made the mistake. Thank you in advance
Given a linked list called "items" that contains 5 nodes with the following data: 1 2 3 4
Goal Delete the last number ( 4 )
Code
nodeType *p=items,*q;
while( p->link!=nullptr)
{
p = p->link;
cout << p->data << " ";
}
// Output of the loop: 2 3 4 ("1" isn't here b/c "p" is overwritten)
// p == address of the third node's link
// p->data == 4
q = p->link; // q == the address of the node that contains 4
p = nullptr; // unlinking the third node from the fourth node.
delete q; // deleting the last node ( that contains 4)
cout << items->link->link->link->data;
// Output: 4
Question
HOW COME NUMBER 4 IS STILL HERE ?!
Aucun commentaire:
Enregistrer un commentaire