Lesson 6 +25 XP

Doubly & Circular Linked Lists

1. Circular Linked List (CLL)

In a Circular Linked List, the next pointer of the last node points back to the first node instead of NULL, forming a closed ring.

  • Key Advantage: Every node is reachable from any starting node, and maintaining a single tail pointer gives O(1) insertion at both front (tail->next) and rear!
  • Insert at Beginning (with tail):
  1. If tail == NULL: newNode->next = newNode; tail = newNode;
  2. Else: newNode->next = tail->next; tail->next = newNode;
  • Insert at End (with tail): Perform front-insert link update, then set tail = newNode;.
  • Delete First Node: temp = tail->next; tail->next = temp->next; free(temp); (handle 1-node case by setting tail = NULL).
  • Traversal Termination: Use a do-while loop stopping when ptr == head.

2. Doubly Linked List (DLL)

Each node stores two pointers: prev (previous node) and next (next node), enabling bidirectional traversal (forward and backward):

C
struct DNode {
    int data;
    struct DNode *prev;
    struct DNode *next;
};

void insertAfter(struct DNode *p, int val) {
    if (!p) return;
    struct DNode *n = (struct DNode*)malloc(sizeof(struct DNode));
    n->data = val;
    n->next = p->next;
    n->prev = p;
    if (p->next != NULL) p->next->prev = n;
    p->next = n;
}

void deleteDNode(struct DNode **headRef, struct DNode *del) {
    if (!*headRef || !del) return;
    if (*headRef == del) *headRef = del->next;
    if (del->next != NULL) del->next->prev = del->prev;
    if (del->prev != NULL) del->prev->next = del->next;
    free(del); // O(1) without traversing for predecessor!
}

3. Circular Doubly Linked List (CDLL)

Combines DLL and CLL invariants: head->prev == tail and tail->next == head.

  • Why CDLL? Eliminates almost all NULL boundary checks when inserting/deleting at ends, and supports O(1) insertion/deletion at both front and back.
  • Real-World Uses: OS round-robin CPU scheduling, browser back/forward history, music playlist repeat loops, and LRU Caches (O(1) eviction).

4. Linked List Variants Comparison

VariantPointers/NodeInsert FrontInsert End (with tail)Delete Given Node
SLL1 (next)O(1)O(1)O(n) (need prev)
CLL1 (next)O(1)O(1)O(n)
DLL2 (prev, next)O(1)O(1)O(1)
CDLL2 (prev, next)O(1)O(1)O(1)

Circular Singly Linked List Algorithms (with START)

In a circular list the last node links back to START, so "end of list" is tested by LINK[PTR] = START instead of NULL.

Insert at the beginning

  1. Get NEW from AVAIL (OVERFLOW if none); INFO[NEW] := ITEM.
  2. If START = NULL: LINK[NEW] := NEW, START := NEW, Exit.
  3. Set PTR := START; repeat PTR := LINK[PTR] while LINK[PTR] ≠ START (find the last node).
  4. Set LINK[NEW] := START, LINK[PTR] := NEW, START := NEW.
  5. Exit.

Insert at the end - steps 1-3 as above, then LINK[NEW] := START and LINK[PTR] := NEW (START is unchanged).

Delete the first node

  1. If START = NULL, UNDERFLOW, Exit.
  2. If LINK[START] = START (one node): free START, START := NULL, Exit.
  3. Find the last node PTR (LINK[PTR] = START).
  4. LINK[PTR] := LINK[START]; free the old START; START := LINK[PTR].
  5. Exit.

Delete the last node - walk SAVE/PTR until LINK[PTR] = START, then LINK[SAVE] := START and free PTR.

💡 Tip

Keeping a pointer to the last node instead of the first makes both "insert at beginning" and "insert at end" O(1), because LINK[last] is the first node.

Applications of Circular Linked Lists

  • Operating systems (round-robin time sharing): the ready processes sit in a circle; the CPU gives each a time slice and moves to the next.
  • Multiplayer games: players take turns in a cycle; after the last player it is the first player's turn again.
  • Music playlists on repeat: after the last song the player returns to the first.
  • Circular buffers / queues in networking and keyboard input.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Write C functions to insert a node after a given node and delete a given node in a Doubly Linked List. Why is deletion O(1)? (5 Marks)
  • Write: struct DNode, insertAfter, deleteDNode, and explain that storing prev eliminates predecessor traversal.
  • Q2. Explain Circular Linked List (CLL) and Circular Doubly Linked List (CDLL) with their applications. (5 Marks)
  • Write: Ring pointer invariants (tail->next == head, head->prev == tail), do-while traversal, and applications (OS round-robin scheduling, LRU cache, playlists).