Implementing a Doubly Linked-List in C
Following on from the discussion on implementing a singly linked-list in C, a logical follow-up data-structure is the doubly linked-list. In a similar fashion, the structure is composed from a set of sequentially linked nodes, each now containing references (pointers) to not only the next node but the previous one to. This structure is useful if the use-case dictates the desire to traverse the list both forwards and backwards, or quickly determine preceding and following elements from a given node. The head and tail nodes can be terminated with either sentinel nodes (referred to as ‘circularly linked’ if only one is used) or like in the implementation shown below, NULL. An observational implementation difference between the two structures is that through storing the previous and next reference, it can significantly simplify the complexity and running time of certain operations (removal from the tail being the most obvious).
Looking at the implementation above you will notice the omission of the list traversal when removing a node from the tail, as the previous nodes reference is already at hand. The ‘list’ method only takes into consideration forward iteration over the list, however, it would be very easy to modify the code to perform backwards traversal. It is good practice to not only free the memory that you do not require anymore, but also NULL any related pointers, as these pointers still have access to the now unowned memory location.
One C implementation detail I would like to discuss is the use of ‘typedef struct’ when declaring the node structure. In C there are two different namespaces for types, a namespace for ‘struct’ tags and one for ‘typedef’ names. Referring to a ‘struct’ can be very verbose and to get around this we can declare both a node ‘struct’ and a plain node in the ‘typedef’ namespace. These both refer to the same type, and we are then able to omit the ‘struct’ keyword. Using only the ‘typedef’ declaration however, would not allow us to perform forward declaration, which gives us the ability to use an identifier before giving the compiler the complete definition.