MCS-208 · Unit 3

Lists

Section 5 of 8

Block 1 · Unit 3Lists14 min · Section 5 of 8

3.5Doubly Linked Lists

Covers IGNOU MCS-208 §3.5

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

A doubly linked list with three nodes
         +------+---+------+     +------+---+------+     +------+---+------+
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).

Insertion after a node p

Four links change. Set the new node’s links first, then fix the neighbours:

  1. n->left = p;
  2. n->right = p->right;
  3. if p->right != NULL: p->right->left = n; (otherwise n is the new tail)
  4. p->right = n;

Deletion of a node p

Because p knows both neighbours, no search is needed:

  1. if p->left != NULL: p->left->right = p->right; else head = p->right;
  2. if p->right != NULL: p->right->left = p->left; else tail = p->left;
  3. free(p);

Singly versus doubly linked

Singly linkedDoubly linked
Links per node1 (next)2 (left, right)
TraversalForwards onlyBoth directions
Delete a node you have a pointer toO(n)O(n): must find its predecessorO(1)O(1)
Insert before a node you haveO(n)O(n)O(1)O(1)
Extra memoryOne pointer per nodeTwo pointers per node
Pointer updates per insertion24

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) 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?

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?

Show answer

Answer: A. 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).

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);?

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
#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;
}
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?

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).

↑ ↓ move↵ openTab switch type