A singly linked list can only be walked forwards, and deleting a node needs a search for its predecessor. Adding a second link in each node, pointing backwards, removes both limits at the cost of one extra pointer per node.
The structure
Intuition
A singly linked list is a one-way street: you can only drive forwards. A doubly linked list is a two-way street: from any house you can go to the next house or back to the previous one.
Definition 3.8 · Doubly linked list
A doubly linked list is a list in which every node has three fields: a pointer to the previous node (left link), the data, and a pointer to the next node (right link). The left link of the first node and the right link of the last node are NULL.
A doubly linked list with three nodes Copy +------+---+------+ +------+---+------+ +------+---+------+
NULL <-- | left | 1 | right| <-> | left | 2 | right| <-> | left | 3 | right| --> NULL
+------+---+------+ +------+---+------+ +------+---+------+
^ head ^ tail
Keeping both a head and a tail pointer makes insertion and deletion at both ends O ( 1 ) O(1) O ( 1 ) .
Insertion after a node p
Four links change. Set the new node’s links first, then fix the neighbours:
n->left = p;
n->right = p->right;
if p->right != NULL: p->right->left = n; (otherwise n is the new tail)
p->right = n;
Deletion of a node p
Because p knows both neighbours, no search is needed:
if p->left != NULL: p->left->right = p->right; else head = p->right;
if p->right != NULL: p->right->left = p->left; else tail = p->left;
free(p);
Example 3.9 Build, traverse both ways, insert and delete
Goal. Build 1 ⇄ 2 ⇄ 3, print it forwards and backwards, insert 25 after 2, delete the first and last nodes, then delete everything.
Program. The list keeps both head and tail.
C · dll.c Copy #include <stdio.h>
#include <stdlib.h>
typedef struct dnode {
struct dnode * left; /* previous node, NULL for the first */
int data;
struct dnode * right; /* next node, NULL for the last */
} DNode;
typedef struct {
DNode * head; /* first node */
DNode * tail; /* last node */
} DList;
DNode * new_dnode( int x)
{
DNode * n = malloc( sizeof * n);
if (n == NULL ) {
fprintf(stderr, "out of memory \n " );
exit(EXIT_FAILURE);
}
n->data = x;
n->left = n->right = NULL ;
return n;
}
void insert_front(DList * l, int x)
{
DNode * n = new_dnode(x);
n->right = l->head;
if (l->head != NULL )
l->head->left = n;
else
l->tail = n; /* list was empty */
l->head = n;
}
void insert_end(DList * l, int x)
{
DNode * n = new_dnode(x);
n->left = l->tail;
if (l->tail != NULL )
l->tail->right = n;
else
l->head = n; /* list was empty */
l->tail = n;
}
/* Insert x after node p (p must be in the list). */
void insert_after(DList * l, DNode * p, int x)
{
DNode * n = new_dnode(x);
n->left = p; /* 1. new node points back to p */
n->right = p->right; /* 2. ... and forward to p's old next */
if (p->right != NULL )
p->right->left = n; /* 3. old next points back to new */
else
l->tail = n;
p->right = n; /* 4. p points forward to new */
}
/* Remove node p from the list and free it. O(1): no search needed. */
void delete_node(DList * l, DNode * p)
{
if (p->left != NULL )
p->left->right = p->right;
else
l->head = p->right; /* p was the first node */
if (p->right != NULL )
p->right->left = p->left;
else
l->tail = p->left; /* p was the last node */
free(p);
}
DNode * search( const DList * l, int key)
{
DNode * p = l->head;
while (p != NULL && p->data != key)
p = p->right;
return p;
}
void print_forward( const DList * l)
{
if (l->head == NULL ) {
printf( "forward : (empty) \n " );
return ;
}
printf( "forward : NULL" );
for (DNode * p = l->head; p != NULL ; p = p->right)
printf( " <-> %d" , p->data);
printf( " <-> NULL \n " );
}
void print_backward( const DList * l)
{
printf( "backward:" );
for (DNode * p = l->tail; p != NULL ; p = p->left)
printf( " %d" , p->data);
printf( " \n " );
}
int main( void )
{
DList l = { NULL , NULL };
insert_end( & l, 2 );
insert_end( & l, 3 );
insert_front( & l, 1 );
print_forward( & l);
print_backward( & l);
insert_after( & l, search( & l, 2 ), 25 );
print_forward( & l);
delete_node( & l, search( & l, 1 )); /* first */
delete_node( & l, search( & l, 3 )); /* last */
print_forward( & l);
print_backward( & l);
while (l.head != NULL )
delete_node( & l, l.head);
print_forward( & l);
return 0 ;
}
Run it.
Output Copy forward : NULL <-> 1 <-> 2 <-> 3 <-> NULL
backward: 3 2 1
forward : NULL <-> 1 <-> 2 <-> 25 <-> 3 <-> NULL
forward : NULL <-> 2 <-> 25 <-> NULL
backward: 25 2
forward : (empty)
Result. Forward and backward traversals agree; insertion after a node and deletion of the first and last nodes keep head and tail correct; deleting all nodes leaves an empty list.
Correction · Program 3.7 (doubly linked list)
Study material says: Program 3.7 (§3.5) includes <malloc.h>, uses void main(), stores −1111 in an extra last node, and traverses backwards with do { … start = start->left; } while (start->right);. Its sample output ends cleanly with “3=2=1=”.
Correct version: Include <stdlib.h> for malloc, use int main(void), create nodes only for real data, keep a tail pointer, and traverse backwards with for (p = tail; p != NULL; p = p->left).
Why: <malloc.h> is not a standard C header (malloc is declared in <stdlib.h>, C17 §7.22.3). In the backward loop, after printing the first node start becomes NULL (the first node’s left link), and the condition start->right then dereferences NULL. A test run of the program printed 3 = 2 = 1 = and then stopped with a segmentation fault, so the printed sample output is not what the program does.
Singly versus doubly linked
Singly linked Doubly linked Links per node 1 (next) 2 (left, right) Traversal Forwards only Both directions Delete a node you have a pointer to O ( n ) O(n) O ( n ) : must find its predecessorO ( 1 ) O(1) O ( 1 ) Insert before a node you have O ( n ) O(n) O ( n ) O ( 1 ) O(1) O ( 1 ) Extra memory One pointer per node Two pointers per node Pointer updates per insertion 2 4
Key idea
Each node of a doubly linked list knows both neighbours, so the list can be walked both ways and any known node can be removed in O ( 1 ) O(1) O ( 1 ) ; the price is one extra pointer per node and more links to keep consistent.
TEE Exam note
For “doubly linked list: definition, insertion and deletion” draw the three-field nodes with arrows in both directions and NULL at both ends; write the four assignments for insertion and the two (plus free) for deletion, with the NULL checks for the first and last node; give the comparison table with the singly linked list.
Common mistake
Updating only the forward links when inserting or deleting. Backward traversal then skips or revisits nodes; always fix both left and right.
Common mistake
Forgetting to update head when the first node is deleted, or tail when the last node is deleted.
Key takeaways
A doubly linked node has left, data and right; the ends hold NULL.
With head and tail pointers, both ends are O ( 1 ) O(1) O ( 1 ) to reach.
Insertion changes 4 links; deletion of a known node changes 2 and needs no search.
Costs: one extra pointer per node and more careful updates.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice How many pointer fields change when a node is inserted between two existing nodes of a doubly linked list?
A 1 B 2 C 3 D 4
Check answer
Show answer Answer: D. 4
The new node’s left and right, the right link of the node before it and the left link of the node after it. 2 is the count for a singly linked list.
Question 2 · multiple choice What is the time to delete a node of a doubly linked list when you already have a pointer to it?
Check answer
Show answer Answer: A. O ( 1 ) O(1) O ( 1 )
The node’s own left and right give its neighbours, so no search is needed. In a singly linked list the same task is O ( n ) O(n) O ( n ) .
Question 3 · multiple choice Node p is the last node of a doubly linked list and is deleted. Which statement is needed besides p->left->right = NULL; and free(p);?
A head = p->right;B tail = p->left;C p->right->left = p->left;D head = NULL;
Check answer
Show answer Answer: B. tail = p->left;
The node before p becomes the last node. p->right is NULL, so p->right->left would dereference NULL.
Question 4 · multiple choice What does this program print?
C · q3-dll.c Copy #include <stdio.h>
typedef struct dnode {
struct dnode * left;
int data;
struct dnode * right;
} DNode;
int main( void )
{
DNode a = { NULL , 1 , NULL }, b = { NULL , 2 , NULL }, c = { NULL , 3 , NULL };
a.right = & b; b.left = & a; b.right = & c; c.left = & b;
b.left->right = b.right; /* unlink b */
b.right->left = b.left;
for (DNode * p = & c; p != NULL ; p = p->left)
printf( "%d " , p->data);
printf( " \n " );
return 0 ;
} A 3 2 1 B 3 1 C 1 3 D 3
Check answer
Show answer Answer: B. 3 1
The two statements link 1 and 3 to each other in both directions, so the backward walk from 3 goes straight to 1. “3 2 1” would mean the left link of 3 was not updated.
Question 5 · multiple choice Which is a disadvantage of a doubly linked list compared with a singly linked list?
A It cannot be traversed backwards. B Deleting a known node needs a search. C Each node needs extra memory for a second pointer. D It cannot have a NULL link.
Check answer
Show answer Answer: C. Each node needs extra memory for a second pointer.
Two links per node cost more memory and more updates. The other options describe the singly linked list’s weaknesses.
Question 6 · short answer
Write the algorithm to insert a new node with value x after the node p in a doubly linked list (with head and tail pointers).
Show worked answer (1) Allocate node n (check malloc); n->data = x. (2) n->left = p; (3) n->right = p->right; (4) If p->right != NULL, set p->right->left = n; otherwise set tail = n (p was the last node). (5) p->right = n;. All steps are O ( 1 ) O(1) O ( 1 ) .