Doubly Linked List in C
In a singly linked list you can only move forward. To delete a node, you must also remember the node before it. A doubly linked list fixes this: every node has two links, one to the next node and one to the previous node, so you can walk both ways.
In this lesson you will build a doubly linked list with head and tail pointers, insert at both ends, insert after a value, delete a node, print the list forward and backward, and compare it with a singly linked list.
What You Will Learn
- The structure of a doubly linked list node.
- Why a tail pointer makes insert and delete at the end O(1).
- How to insert at the beginning, end and after a given value.
- How to delete a node by fixing two links.
- How to traverse forward and backward.
- Singly vs doubly linked list.
Doubly Linked List in C Explained
Exam definition: A doubly linked list is a linear data structure in which each node contains three fields: a prev pointer to the previous node, the data, and a next pointer to the next node. The prev of the first node and the next of the last node are NULL. It can be traversed in both directions, and a node can be deleted using only a pointer to that node.
Memory trick: A doubly linked list is a metro train with doors on both ends of every coach. From any coach you can walk to the next coach or back to the previous one. The driver's cabins at the two ends (head and tail) have no coach beyond them (NULL).
head tail
│ │
▼ ▼
┌──────┬────┬───┐ ┌───┬────┬───┐ ┌───┬────┬──────┐
│ NULL │ 10 │ ●─┼──►│ ● │ 20 │ ●─┼──►│ ● │ 30 │ NULL │
└──────┴────┴───┘◄──┼─● │ │ │◄──┼─● │ │ │
prev data next └───┴────┴───┘ └───┴────┴──────┘
Syntax of a Doubly Linked List Node
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
struct dlist
{
struct dnode *head; /* first node */
struct dnode *tail; /* last node */
};
/* Unlinking node x (any position): */
if (x->prev) x->prev->next = x->next; else head = x->next;
if (x->next) x->next->prev = x->prev; else tail = x->prev;
free(x);
Example 1: Doubly Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
struct dlist
{
struct dnode *head;
struct dnode *tail;
};
struct dnode *new_node(int value)
{
struct dnode *n = malloc(sizeof *n);
if (n == NULL)
exit(1);
n->prev = n->next = NULL;
n->data = value;
return n;
}
void insert_front(struct dlist *l, int value)
{
struct dnode *n = new_node(value);
n->next = l->head;
if (l->head != NULL)
l->head->prev = n;
else
l->tail = n; /* list was empty */
l->head = n;
}
void insert_end(struct dlist *l, int value)
{
struct dnode *n = new_node(value);
n->prev = l->tail;
if (l->tail != NULL)
l->tail->next = n;
else
l->head = n; /* list was empty */
l->tail = n;
}
int insert_after(struct dlist *l, int key, int value)
{
struct dnode *p, *n;
for (p = l->head; p != NULL && p->data != key; p = p->next)
;
if (p == NULL)
return 0;
n = new_node(value);
n->prev = p;
n->next = p->next;
if (p->next != NULL)
p->next->prev = n;
else
l->tail = n;
p->next = n;
return 1;
}
int delete_value(struct dlist *l, int value)
{
struct dnode *x;
for (x = l->head; x != NULL && x->data != value; x = x->next)
;
if (x == NULL)
return 0;
if (x->prev != NULL) x->prev->next = x->next; else l->head = x->next;
if (x->next != NULL) x->next->prev = x->prev; else l->tail = x->prev;
free(x);
return 1;
}
void print_forward(const struct dlist *l)
{
const struct dnode *p;
printf("Forward : ");
for (p = l->head; p != NULL; p = p->next)
printf("%d ", p->data);
printf("\n");
}
void print_backward(const struct dlist *l)
{
const struct dnode *p;
printf("Backward: ");
for (p = l->tail; p != NULL; p = p->prev)
printf("%d ", p->data);
printf("\n");
}
void free_list(struct dlist *l)
{
struct dnode *p = l->head, *next;
while (p != NULL)
{
next = p->next;
free(p);
p = next;
}
l->head = l->tail = NULL;
}
int main(void)
{
struct dlist list = {NULL, NULL};
insert_end(&list, 20);
insert_end(&list, 40);
insert_front(&list, 10);
insert_after(&list, 20, 30);
print_forward(&list);
print_backward(&list);
delete_value(&list, 10); /* first node */
delete_value(&list, 30); /* middle node */
delete_value(&list, 40); /* last node */
printf("After deleting 10, 30, 40:\n");
print_forward(&list);
print_backward(&list);
free_list(&list);
return 0;
}
Output
Forward : 10 20 30 40 Backward: 40 30 20 10 After deleting 10, 30, 40: Forward : 20 Backward: 20
Code Explanation
struct dlistkeeps bothheadandtail. With the tail, adding at the end is O(1); no walking is needed.insert_frontlinks the new node before the old head and points the old head'sprevback to it. If the list was empty, the new node is also the tail.insert_after(&list, 20, 30)sets four links: new→prev = 20, new→next = 40, 40→prev = new, 20→next = new.delete_valueneeds no separate prev pointer: each node already knows its neighbours. Two lines fix the links on both sides, with special cases for the first and last node.print_backwardstarts at the tail and followsprevlinks, something a singly linked list can't do.
How Insertion Works in a Doubly Linked List
Insert N between A and B:
before: A ⇄ B
1. N.prev = A A ⇄ B, A ◄─ N
2. N.next = B A ⇄ B, A ◄─ N ─► B
3. B.prev = N A ─► B, N ⇄ B
4. A.next = N A ⇄ N ⇄ B (done: 4 links changed)
Singly vs doubly linked list
| Point | Singly | Doubly |
|---|---|---|
| Links per node | 1 (next) | 2 (prev, next) |
| Traversal | Forward only | Forward and backward |
| Delete a given node | Needs the previous node | Possible with just that node |
| Memory per node | Less | One extra pointer |
| Code complexity | Simpler | More links to update |
| Typical use | Stacks, simple lists | Browser back/forward, music player, LRU cache |
Example 2: Browser History with Back and Forward
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct page
{
struct page *prev;
char url[40];
struct page *next;
};
struct page *visit(struct page *current, const char *url)
{
struct page *p = malloc(sizeof *p), *old, *t;
if (p == NULL)
exit(1);
strcpy(p->url, url);
p->prev = current;
p->next = NULL;
if (current != NULL)
{
old = current->next; /* visiting a new page drops "forward" pages */
while (old != NULL)
{
t = old->next;
free(old);
old = t;
}
current->next = p;
}
printf("Visit : %s\n", url);
return p;
}
int main(void)
{
struct page *cur = NULL, *t;
cur = visit(cur, "studyfame.com");
cur = visit(cur, "studyfame.com/c-tutorial");
cur = visit(cur, "studyfame.com/c-program");
cur = cur->prev;
printf("Back : %s\n", cur->url);
cur = cur->prev;
printf("Back : %s\n", cur->url);
cur = cur->next;
printf("Forward : %s\n", cur->url);
cur = visit(cur, "studyfame.com/mcq");
printf("Forward possible? %s\n", cur->next ? "yes" : "no");
while (cur->prev != NULL) /* go to the first page, then free all */
cur = cur->prev;
while (cur != NULL)
{
t = cur->next;
free(cur);
cur = t;
}
return 0;
}
Output
Visit : studyfame.com Visit : studyfame.com/c-tutorial Visit : studyfame.com/c-program Back : studyfame.com/c-tutorial Back : studyfame.com Forward : studyfame.com/c-tutorial Visit : studyfame.com/mcq Forward possible? no
- Each page is a node; Back follows
prevand Forward followsnext. - Visiting a new page after going back removes the old "forward" pages, just like a real browser.
- This back-and-forth movement is exactly what a doubly linked list is good at.
Important Points to Remember
- Node = prev + data + next.
- First node's prev = NULL; last node's next = NULL.
- Keep a tail pointer for O(1) insert/delete at the end.
- Insertion in the middle changes 4 links; deletion changes 2 links.
- Always update head/tail when the first or last node changes.
- Costs one extra pointer per node, but allows backward traversal and easy deletion.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Updating next but forgetting prev | Backward traversal is wrong or crashes | Every link change has a partner: next and prev |
| Not updating tail when deleting the last node | tail points to freed memory | else l->tail = x->prev; |
x->next->prev = ... when x is last | NULL pointer dereference | Check x->next != NULL first |
| Inserting into an empty list without setting both head and tail | One of them stays NULL | Handle the empty case separately |
| Freeing a node, then reading its next | Undefined behavior | Save next before free |
Doubly Linked List Practice Questions
- What is a doubly linked list? Draw a node.
- Give two advantages and one disadvantage compared to a singly linked list.
- How many links change when inserting in the middle? When deleting?
- Why does a tail pointer help?
- Name two real applications of a doubly linked list.
Show answers
- A list where each node has prev, data and next fields: [prev | data | next].
- Advantages: traversal in both directions; deleting a node needs only that node. Disadvantage: extra memory and more pointer updates.
- Four links when inserting; two links when deleting.
- Insert and delete at the end become O(1), and backward traversal can start immediately.
- Browser back/forward history, music player playlist, undo/redo, LRU cache.
Coding Exercise
Write int is_palindrome(struct dlist *l) that checks whether a doubly linked list of integers reads the same forward and backward, using one pointer from the head and one from the tail. Test it with 1 2 3 2 1 and 1 2 3 4.
Show solution
#include <stdio.h>
#include <stdlib.h>
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
};
struct dlist
{
struct dnode *head, *tail;
};
void add(struct dlist *l, int v)
{
struct dnode *n = malloc(sizeof *n);
if (n == NULL)
exit(1);
n->data = v;
n->next = NULL;
n->prev = l->tail;
if (l->tail) l->tail->next = n; else l->head = n;
l->tail = n;
}
int is_palindrome(const struct dlist *l)
{
const struct dnode *a = l->head, *b = l->tail;
while (a != b && a->prev != b) /* stop when they meet or cross */
{
if (a->data != b->data)
return 0;
a = a->next;
b = b->prev;
}
return 1;
}
void clear(struct dlist *l)
{
struct dnode *p = l->head, *t;
while (p) { t = p->next; free(p); p = t; }
l->head = l->tail = NULL;
}
int main(void)
{
struct dlist l = {NULL, NULL};
int x[] = {1, 2, 3, 2, 1}, y[] = {1, 2, 3, 4};
int i;
for (i = 0; i < 5; i++) add(&l, x[i]);
printf("1 2 3 2 1 -> %s\n", is_palindrome(&l) ? "palindrome" : "not palindrome");
clear(&l);
for (i = 0; i < 4; i++) add(&l, y[i]);
printf("1 2 3 4 -> %s\n", is_palindrome(&l) ? "palindrome" : "not palindrome");
clear(&l);
return 0;
}
1 2 3 2 1 -> palindrome 1 2 3 4 -> not palindrome
Quick Quiz: Doubly Linked List (MCQ)
-
A doubly linked list node has how many pointers?
A) 0 B) 1 C) 2 D) 3
-
The prev pointer of the first node is:
A) tail B) NULL C) head D) itself
-
With a tail pointer, inserting at the end takes:
A) O(n) B) O(1) C) O(log n) D) O(n²)
-
Which can be traversed backward?
A) Singly linked list B) Doubly linked list C) Both D) Neither
-
Deleting a node from the middle of a doubly linked list changes how many links?
A) 1 B) 2 C) 3 D) 4
Show answers
- C: 2 (prev and next).
- B: NULL.
- B: O(1).
- B: doubly linked list.
- B: 2.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Node | prev + data + next |
|---|---|
| Ends | head→prev = NULL, tail→next = NULL |
| Traversal | Both directions |
| Insert in middle | 4 links |
| Delete | 2 links, no extra prev pointer needed |
| Tail pointer | End operations in O(1) |
| Cost | Extra pointer per node |
Frequently asked questions
- What is a doubly linked list?
- A list whose nodes have prev and next pointers, so it can be traversed forward and backward.
- What is the main advantage over a singly linked list?
- Backward traversal and deleting a node without knowing the previous node.
- Where is a doubly linked list used?
- In browser back/forward, music playlists, undo/redo and LRU caches.
Remember
- Doubly linked list = metro coaches with doors at both ends.
- Every next has a partner prev: update both.
Next Topic
Next lesson: Circular Linked List in C. You will join the last node back to the first to make a circular linked list.