Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 2 · syllabusAsked 3× in KTU papersLesson live

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
ormakkurippu ✎
Never-forget card

DLL node = PREV ← DATA → NEXT. Every insert fixes FOUR links; draw arrows before writing code.

Picture it

A KSRTC bus with doors at both ends — you can walk to the next seat or back to the previous one.

Write this in the exam

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

C
struct dnode {
    struct dnode *prev;
    int data;
    struct dnode *next;
};
head102030NULL
Black arrows: next. Red arrows: prev. head→prev and last→next are NULL.

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 ◀ ▶.

Loading visualizer…

Algorithms

algorithm
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→prev

C implementation

doubly_linked_list.ctested ✓ 160/160 cases
C
/* 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

Open in the Code Lab →

SLL vs DLL — head and tail insertion (model paper Q3)

Singly linkedDoubly linked
Insert at headO(1): 2 pointer changesO(1): up to 4 pointer changes
Insert at tail, no tail pointerO(n)O(n)
Insert at tail, with tail pointerO(1)O(1)
Delete a given nodeO(n) (must find prev)O(1)
Traverse backwardsnot possibleyes
Memory per nodedata + 1 pointerdata + 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)

Quick check — can you get them all?

Question 1

Q1.To display a DLL in reverse (Nov 2025 Q12b) you…

Question 2

Q2.Insert at the beginning of a non-empty DLL needs…

Question 3

Q3.Extra cost of a DLL compared with an SLL?

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)