Thursday, November 19, 2009

If a linked list says something like node 2 = node 4 would that delete node 3, if not what would it do?

If a linked list Says something like node 2 = node 4 would that delete node 3, if not what would it do?

If a linked list says something like node 2 = node 4 would that delete node 3, if not what would it do?
Not necessarily. The way you have it written means that node 4 would get copied into node 2, and that would really screw up your list.





The way linked lists are usually coded, you have:


node1-%26gt;next = node2;


node2-%26gt; next = node 3;


node3-%26gt;next = node 4;


node4-%26gt;next = NULL;   /* end of list */


That is, the next member of the node structure would point to the next node in the list.





If you wanted to remove node3 from the list, you would set:


node2-%26gt;next = node4;


Note that the memory allocated to node3 would still be out there floating around, which is kind of messy.
Reply:Although if you're talking about the pointer (inside node 1) being replaced from 2 to 4, then it would remove both 2 and 3.


No comments:

Post a Comment