Doubly linked lists
Two links per node — walk forward AND backward; insert or delete without hunting for the previous node.
Syllabus says: “Doubly Linked List”
On this page · 12 sections
DLL node = PREV ← DATA → NEXT. Every insert fixes FOUR links; draw arrows before writing code.
A KSRTC bus with doors at both ends — you can walk to the next seat or back to the previous one.
In a doubly linked list each node has data and two pointers, prev and next, so the list can be traversed in both directions and a node can be deleted given only its own address.
What you will be able to do
- Define a DLL node.
- Insert at the beginning and at the end (May 2026 Q11b).
- Delete a node; display in reverse (Nov 2025 Q12b).
- Compare head/tail insertion in SLL and DLL (model paper Q3).
The node
struct dnode {
struct dnode *prev;
int data;
struct dnode *next;
};Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithms
Algorithm InsertFront(x)
1. new ← getNode(x); new→prev ← NULL
2. new→next ← head
3. if head ≠ NULL then head→prev ← new
4. head ← new
Algorithm InsertEnd(x)
1. new ← getNode(x); new→next ← NULL
2. if head = NULL then new→prev ← NULL; head ← new; return
3. p ← head; while p→next ≠ NULL do p ← p→next
4. p→next ← new; new→prev ← p
Algorithm DeleteNode(p) // p points to the node to delete
1. if p→prev ≠ NULL then p→prev→next ← p→next else head ← p→next
2. if p→next ≠ NULL then p→next→prev ← p→prev
3. free(p)
Algorithm DisplayReverse()
1. if head = NULL then print "Empty"; return
2. p ← head; while p→next ≠ NULL do p ← p→next // go to the last node
3. while p ≠ NULL do print p→data; p ← p→prevC implementation
doubly_linked_list.ctested ✓ 160/160 cases/* Doubly linked list — KTU PCCST303, Module 2
* Each node has prev and next, so the list can be walked in both directions.
* Commands: IF x | IE x | IA k x | D x | S x | DR (display in reverse, KTU Nov 2025 Q12b)
* After every command: optional result line, "LIST: ..." (via next) and
* "BACK: ..." (from the last node via prev — checks every prev pointer). */
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *prev;
int data;
struct node *next;
};
struct node *head = NULL;
struct node *getNode(int x) {
struct node *n = malloc(sizeof *n);
if (n == NULL) { printf("OUT OF MEMORY\n"); exit(1); }
n->data = x;
n->prev = n->next = NULL;
return n;
}
void insertFront(int x) {
struct node *n = getNode(x);
n->next = head;
if (head != NULL) head->prev = n; /* old first node points back to the new one */
head = n;
}
void insertEnd(int x) {
struct node *n = getNode(x), *p;
if (head == NULL) { head = n; return; }
for (p = head; p->next != NULL; p = p->next) { }
p->next = n;
n->prev = p;
}
void insertAfter(int key, int x) {
struct node *p = head, *n;
while (p != NULL && p->data != key) p = p->next;
if (p == NULL) { printf("key %d not found\n", key); return; }
n = getNode(x);
n->prev = p; /* 4 pointer changes */
n->next = p->next;
if (p->next != NULL) p->next->prev = n;
p->next = n;
}
void deleteKey(int x) {
struct node *p = head;
if (head == NULL) { printf("UNDERFLOW\n"); return; }
while (p != NULL && p->data != x) p = p->next;
if (p == NULL) { printf("%d not found\n", x); return; }
if (p->prev != NULL) p->prev->next = p->next; else head = p->next;
if (p->next != NULL) p->next->prev = p->prev;
free(p);
}
void search(int x) {
struct node *p = head;
int pos = 1;
for (; p != NULL; p = p->next, pos++)
if (p->data == x) { printf("found at position %d\n", pos); return; }
printf("%d not found\n", x);
}
void displayReverse(void) {
struct node *p = head;
if (head == NULL) { printf("empty list\n"); return; }
while (p->next != NULL) p = p->next; /* go to the last node */
printf("reverse:");
for (; p != NULL; p = p->prev) printf(" %d", p->data);
printf("\n");
}
void display(void) {
struct node *p, *last = NULL;
printf("LIST:");
for (p = head; p != NULL; p = p->next) { printf(" %d", p->data); last = p; }
printf("\nBACK:");
for (p = last; p != NULL; p = p->prev) printf(" %d", p->data);
printf("\n");
}
int main(void) {
char cmd[8];
int a, b;
while (scanf("%7s", cmd) == 1) {
if (cmd[0] == 'I' && cmd[1] == 'F' && scanf("%d", &a) == 1) insertFront(a);
else if (cmd[0] == 'I' && cmd[1] == 'E' && scanf("%d", &a) == 1) insertEnd(a);
else if (cmd[0] == 'I' && cmd[1] == 'A' && scanf("%d %d", &a, &b) == 2) insertAfter(a, b);
else if (cmd[0] == 'D' && cmd[1] == 'R') displayReverse();
else if (cmd[0] == 'D' && scanf("%d", &a) == 1) deleteKey(a);
else if (cmd[0] == 'S' && scanf("%d", &a) == 1) search(a);
else { printf("BAD COMMAND\n"); break; }
display();
}
while (head != NULL) { struct node *t = head; head = head->next; free(t); }
return 0;
}
Sample run · display in reverse (Nov 2025 Q12b) · real output from gcc
Input (stdin)
IE 10 IE 20 IF 5 IA 10 15 D 20 DR
Output (stdout)
LIST: 10 BACK: 10 LIST: 10 20 BACK: 20 10 LIST: 5 10 20 BACK: 20 10 5 LIST: 5 10 15 20 BACK: 20 15 10 5 LIST: 5 10 15 BACK: 15 10 5 reverse: 15 10 5 LIST: 5 10 15 BACK: 15 10 5
SLL vs DLL — head and tail insertion (model paper Q3)
| Singly linked | Doubly linked | |
|---|---|---|
| Insert at head | O(1): 2 pointer changes | O(1): up to 4 pointer changes |
| Insert at tail, no tail pointer | O(n) | O(n) |
| Insert at tail, with tail pointer | O(1) | O(1) |
| Delete a given node | O(n) (must find prev) | O(1) |
| Traverse backwards | not possible | yes |
| Memory per node | data + 1 pointer | data + 2 pointers |
Common mistakes
- Forgetting to update the prev pointer of the old head.
- Not handling the empty list in InsertEnd.
- Deleting the head without moving head.
Asked in KTU exams (3)
- November 2025 · Q12b3 marksWrite algorithm to display elements in a doubly linked list in reverse order.
- May 2026 · Q11b4 marksWrite algorithm for insert node at the beginning and end of a doubly linked list.
- — 2024 · Q33 marksCompare the insertion at head and tail in singly linked list and doubly linked list
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Node: prev, data, next.
- Front insert: new→next = head; head→prev = new; head = new.
- Reverse display: go to the last node, follow prev.
- Delete a node in O(1) given its address.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §4.8 (Doubly linked lists)
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §10.2
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)