Loading lessons...
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
tailpointer gives O(1) insertion at both front (tail->next) and rear! - Insert at Beginning (with
tail):
- If
tail == NULL:newNode->next = newNode; tail = newNode; - Else:
newNode->next = tail->next; tail->next = newNode;
- Insert at End (with
tail): Perform front-insert link update, then settail = newNode;. - Delete First Node:
temp = tail->next; tail->next = temp->next; free(temp);(handle 1-node case by settingtail = NULL). - Traversal Termination: Use a
do-whileloop stopping whenptr == head.
2. Doubly Linked List (DLL)
Each node stores two pointers: prev (previous node) and next (next node), enabling bidirectional traversal (forward and backward):
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
NULLboundary 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
| Variant | Pointers/Node | Insert Front | Insert End (with tail) | Delete Given Node |
|---|---|---|---|---|
| SLL | 1 (next) | O(1) | O(1) | O(n) (need prev) |
| CLL | 1 (next) | O(1) | O(1) | O(n) |
| DLL | 2 (prev, next) | O(1) | O(1) | O(1) |
| CDLL | 2 (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
- Get NEW from AVAIL (OVERFLOW if none); INFO[NEW] := ITEM.
- If START = NULL: LINK[NEW] := NEW, START := NEW, Exit.
- Set PTR := START; repeat PTR := LINK[PTR] while LINK[PTR] ≠ START (find the last node).
- Set LINK[NEW] := START, LINK[PTR] := NEW, START := NEW.
- 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
- If START = NULL, UNDERFLOW, Exit.
- If LINK[START] = START (one node): free START, START := NULL, Exit.
- Find the last node PTR (LINK[PTR] = START).
- LINK[PTR] := LINK[START]; free the old START; START := LINK[PTR].
- Exit.
Delete the last node - walk SAVE/PTR until LINK[PTR] = START, then LINK[SAVE] := START and free PTR.
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.
📝 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 storingpreveliminates 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-whiletraversal, and applications (OS round-robin scheduling, LRU cache, playlists).