Doubly Linked List
A doubly linked list stores prev and next on every node. You can walk either direction and delete a node in O(1) if you already have a pointer to it.
Detailed Explanation
A singly linked list can delete the next node cheaply. Deleting the current node means finding the previous one, which is a walk from the head. A doubly linked list stores that previous pointer:
struct Node {
int value;
struct Node *prev;
struct Node *next;
};
Unlinking p is p->prev->next = p->next and p->next->prev = p->prev, with care at the ends. That is O(1) and is the reason kernels and allocators like this shape: given a pointer to an object, you can remove it from a list without searching.
The extra pointer costs space and costs attention. Every splice has to update four links in the interior and two at an end. One missed assignment leaves a one-way link: walking forward and walking backward visit different sequences. Tests should walk both ways.
Sentinel nodes simplify the ends. A dummy head whose next is the first real element and whose prev is the last — or a ring where the sentinel points at itself when empty — means every real node has non-NULL neighbors. The insert and delete code paths no longer special-case head. The sentinel is not an element; do not free it with the data nodes, or do not allocate it separately if it lives in the list struct.
A circular doubly linked list drops NULL entirely: the last next is the first, the first prev is the last. Walking needs a start pointer and a stop condition that is not NULL, usually p != head after one step. Forgetting that condition is an infinite loop.
Ownership is the same as the singly linked case. Nodes are allocated and must be freed. A function that unlinks a node should say whether it also frees it. Two lists should never share a node unless you are writing an intrusive structure and know the object is in one list at a time.
Code Examples
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *prev;
struct Node *next;
};
static struct Node *node_new(int value)
{
struct Node *n = malloc(sizeof *n);
if (n == NULL) {
return NULL;
}
n->value = value;
n->prev = n->next = NULL;
return n;
}
static void unlink_node(struct Node **head, struct Node *p)
{
if (p->prev != NULL) {
p->prev->next = p->next;
} else {
*head = p->next;
}
if (p->next != NULL) {
p->next->prev = p->prev;
}
p->prev = p->next = NULL;
}
int main(void)
{
struct Node *a = node_new(1);
struct Node *b = node_new(2);
if (a == NULL || b == NULL) {
free(a);
free(b);
return 1;
}
a->next = b;
b->prev = a;
struct Node *head = a;
unlink_node(&head, a);
printf("%d\n", head->value); /* prints: 2 */
free(a);
free(b);
return 0;
}Common Gotchas
Updating only `next` leaves a stale `prev`
the next walk backward will visit a node you thought you removed.
Deleting the head must update the caller's head pointer
pass `struct Node **head` or return the new head.
A circular list has no `NULL` end
`while (p != NULL)` never stops. Stop when you return to the start.
`O(1)` delete requires a pointer to the node
finding the node by value is still `O(n)`.