Archived

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

Head and Tail Nodes, really needed?

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are head and tail nodes really needed or have some underlying importance that I''m not seeing in a list? They just seem to be extra weight and not really serving a purpose.

Share this post


Link to post
Share on other sites
Basically, the reason you have head and tail nodes in a list is so that you don''t have to worry so much about special cases. Take removing an element from the list for example. Generally if you''re removing an element you will do something like this:

prev->next = cur->next;

However if you want to remove the first element in the list, you don''t have a previous element, so a dummy head element is put in. If it wasn''t there you would have to make sure the element you''re removing isn''t the first element (it just makes things a little bit easier).

Give a man a fish and you feed him for a day; teach him to use the Net and he won''t bother you for weeks.

Share this post


Link to post
Share on other sites
It's also easier to delete the list entrys if you have the two "end-pointers" saved... delete as in new (or free if you want)

[edited by - Rickmeister on June 3, 2002 12:40:02 AM]

Share this post


Link to post
Share on other sites
They''re not really needed, as you can just maintain pointers to the real head and tail.

They are, however, convenient. You no longer have to worry about what happens when you have an empty list. And with circular linked lists, a single sentinel (which does double duty as the head and tail) is a convenient place to hold onto the list anyway.

Share this post


Link to post
Share on other sites