2

Theory

When deleting a node from a linked list, pointers to the first node in the list will need to be updated if the node being deleted from the list is the first one.

Background

Libevent: how to close all open sockets on shutdown?

Situation

In my server application, a pointer to the first node in a linked list of socket connections is held in a struct holding data related to the context of the running instance, such as listening socket port and so on. When a connection is closed, the related node in the linked list must be removed which means that the function which deletes the node must also access to the instance context struct.

My first ideas were:

Each connection node in the linked list has a pointer to the instance context struct. (Messy.)

Global variable pointer to instance context struct. (Evil.)

Then I had the idea to make the first node in the linked list a sentinel node thereby avoiding the possibility that the first node would ever be removed and therefore side-stepping the need for the socket close function to have access to the instance context.

Question

Is this a suitable use of sentinel nodes or is there a better way to solve this problem?

1
  • Positing a simply implementation of your idea would greatly help. Maybe post on codereview.stackexchange.com Ideas: 1) Yes the sentinel idea can work 2) Still need a special delete() to delete the linked list along with the sentinel node. 3) Using this technique should code have high percentage of empty lists is space inefficient. Commented Feb 17, 2014 at 15:48

1 Answer 1

0

Is there any meta data about the linked list you'd like to store such as the length of the list? If so, you can store them in the sentinel node.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.