Archived

This topic is now archived and is closed to further replies.

zaNeXuS

Deleting nodes in Linked lists

Recommended Posts

Hey guys!! Listen I quickly have to make sure of something here... Ok say i got a linked list, with a HEAD pointer and a TAIL pointer. Now say i got 5 nodes in that list and I want to delete the 3rd one. My code for deleting the actual node from the list works fine ( i think) if u guys want to see it... I#ll gladly post it, but my question is as follows... I got a class, and in the class is a pointer which holds reference to the 3rd node for instance. If I had to delete that 3rd node in My Delete_Node function. Would that make the pointer in my class NULL as well? Or should I delete that one too? The latter question doesn''t make sense to me, cos it''s already a reference and surely if that memory gets cleared up, that pointer can''t be pointing to that memory space any more. Thx for your time!!

Share this post


Link to post
Share on other sites
quote:
Original post by zaNeXuS
Hey guys!!

Listen I quickly have to make sure of something here...

Ok say i got a linked list, with a HEAD pointer and a TAIL pointer. Now say i got 5 nodes in that list and I want to delete
the 3rd one.

My code for deleting the actual node from the list works fine ( i think) if u guys want to see it... I#ll gladly post it, but my question is as follows...

I got a class, and in the class is a pointer which holds reference to the 3rd node for instance. If I had to delete that 3rd node in My Delete_Node function. Would that make the pointer in my class NULL as well? Or should I delete that one too? The latter question doesn''t make sense to me, cos it''s already a reference and surely if that memory gets cleared up, that pointer can''t be pointing to that memory space any more.

Thx for your time!!





The fist thing to dois to have a previous and a next ... so that u will store the in previous the address of the 2nd item and in next the address of the 4th item. Then after performing the deletion you can then use previous->link to point to next

Share this post


Link to post
Share on other sites
First of all, deleting a pointer just frees up the memory it was pointing to and makes it a dangling pointer. You have to make it NULL yourself. As far as any other pointers that were pointing to that node, those also become dangling as well. They wont work and might crash the program if you try to dereference them. As a general rule, if you ever delete a pointer, make sure its not being referenced by any other pointers unless you can set everything else that referenced to it NULL as well(or reassign it).

For clarification: are you saying that your class is a linked list and has a pointer to the next node, and at the same time, the head node has pointers to every node within it? If you are doing that, its not a good idea. You might want to put up some code for the class and the delete node function.

Share this post


Link to post
Share on other sites
Ahh yes!!

Ok Ebony, thx for that, I do have prev and next. Without that it would really be a problem offcourse.

Metal. This is just the answer I was looking for. Those dangling pointers would be my problem. So all I have to do is assign ,any other pointers which references that specific node, to NULL after I actually removed it from the list then ?

If so, wow, basic enough!! Thanx alot for the info guys!!

"For clarification: are you saying that your class ......" <<--->> No, the class is any arbitary one, which will have a pointer within the class(a variable) which has reference to that 3rd node.

Happy programming!

Share this post


Link to post
Share on other sites