Download Circular Linked List Data Structure Notes – Insertion, Deletion, Traversal & Uses (C Code) and more Study notes Computer Science in PDF only on Docsity!
DATA STRUCTURES
Circular Linked List — Complete Notes
(Language used for code: C)
What You'll Master in These Notes
- Why the last node points^ back to the first node^ instead of NULL — and what that changes
- Circular Singly vs Circular Doubly Linked List — structure and key differences
- Insertion & Deletion at beginning, end, and middle — with pointer-level diagrams
- Traversal logic that correctly detects the loop back (a common beginner bug explained)
- Real-world^ uses:^ round-robin^ CPU^ scheduling,^ multiplayer^ turn^ cycling,^ and^ the^ Josephus
Problem
- 5 fully solved, step-by-step problems with pointer traces
This is a fact-checked, 15-page guide — every time complexity claim includes the reasoning behind it,
and every diagram shows exactly how the pointers connect in a loop.
The Data Structure Behind "Whose Turn Is It Next?"
Most data structures model a line — a beginning and a definite end. A Circular Linked List
models something different: a cycle , where after the last item, you land right back on the first
one. This single change in structure turns out to be exactly what a surprising number of real
systems need.
Round-Robin CPU Scheduling
Operating systems commonly use the Round Robin scheduling algorithm, where each process is given
a fixed time slice on the CPU before control moves to the next process in line — and after the last
process, control cycles back to the first one. This is conceptually a circular structure: a queue that wraps
around instead of ending.
Multiplayer Games and Turn-Based Systems
In turn-based games — board games, card games, or multiplayer strategy games — play passes from
one player to the next, and after the last player's turn, it cycles back to the first player. Modeling this
rotation as a circular list is a natural fit, since 'who goes next' is always just one step forward, with
wraparound handled automatically.
The Josephus Problem — A Famous Puzzle
A well-known computer science problem asks: if people stand in a circle and every k-th person is
eliminated in turn, who survives? This puzzle, called the Josephus Problem, is a classic example used to
teach circular linked lists, because the elimination process — moving around a circle and skipping or
removing nodes — maps directly onto circular list operations.
Buffering and Cyclic Data Streams
Systems that continuously cycle through a fixed set of slots — such as circular buffers used in audio and
data streaming — rely on the same wrap-around principle, even though such buffers are often
implemented with arrays rather than linked lists in practice.
Keep these examples in mind as you go through the structure, operations, and diagrams ahead —
the 'wrap-around' pointer logic explained in this document is the exact mechanism behind all of them.
1. Structure (Last Node Points Back to First Node)
A Circular Linked List is a linked list in which the last node, instead of storing NULL in its next pointer,
stores the address of the first node (HEAD). This means there is no true 'end' to the list — starting from
any node and repeatedly following next pointers will eventually cycle back through every node and
return to the starting point.
Diagram: Structure of a Circular Linked List
40 loops back
HEAD
Figure 1: Four nodes connected in a loop. Following the next pointer from the last node (40) leads back to HEAD (10)
instead of NULL.
1.1 Key Points
- There is no NULL pointer anywhere in a non-empty circular linked list — this is the single biggest
structural difference from a linear (singly/doubly) linked list.
- HEAD is still a separate pointer variable that marks the conventional 'starting point,' even though
the list itself has no fixed beginning or end.
- An empty circular linked list is represented by HEAD = NULL, exactly as in a linear linked list.
- A list with a single node is a special case: that node's next pointer points to itself.
2. Types: Circular Singly Linked List vs Circular
Doubly Linked List
Just as with linear linked lists, a circular linked list can be built using nodes with a single pointer (next
only) or with two pointers (next and prev). This gives rise to two variants.
2.1 Circular Singly Linked List
Each node has only a next pointer. The list can only be traversed in one direction, but because it is
circular, continuing forward from the last node leads back to the first node instead of terminating at
NULL.
2.2 Circular Doubly Linked List
Each node has both a next and a prev pointer. The last node's next points to the first node, and the first
node's prev points to the last node — so the loop exists in both directions. This allows traversal forward
or backward from any node, with wrap-around in both directions.
Diagram: Circular Doubly Linked List structure
next (green) prev (red)
Figure 2: Green arrows show the next-direction loop; red arrows show the prev-direction loop — both cycle
continuously with no NULL anywhere.
4. Insertion Operations (Circular Singly Linked List)
Important accuracy note: Unlike a linear singly linked list, insertion at the beginning of a circular linked
list is not O(1) unless a TAIL pointer is separately maintained. This is because the last node's next
pointer must be updated to point to the new HEAD, and finding the last node normally requires
traversing the entire list.
4.1 Insertion at Beginning
struct Node* insertAtBeginning(struct Node* head, int value) { struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); newNode->data = value;
if (head == NULL) { // empty list newNode->next = newNode; // points to itself return newNode; }
struct Node* last = head; while (last->next != head) { // find the last node last = last->next; } newNode->next = head; last->next = newNode; // last node now points to the new head head = newNode; return head; }
Time Complexity: O(n) — traversal is required to find the last node and update its pointer. (O(1) is
possible only if a TAIL pointer is separately maintained.)
4.2 Insertion at End
struct Node* insertAtEnd(struct Node* head, int value) { struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); newNode->data = value;
if (head == NULL) { newNode->next = newNode; return newNode; }
struct Node* last = head; while (last->next != head) { last = last->next; } last->next = newNode; newNode->next = head; // new last node wraps back to head return head; }
Diagram: Insertion at end
Before: HEAD -> 10 -> 20 -> 30 -> (back to 10)
After: insert 40 at end -> HEAD -> 10 -> 20 -> 30 -> 40 -> (back to 10)
HEAD
Figure 3: A new node (40) is linked after the old last node (30), and 40's next is set to wrap back to HEAD (10).
Time Complexity: O(n) — the list must be traversed to find the current last node.
5. Deletion Operations (Circular Singly Linked List)
5.1 Deletion at Beginning
struct Node* deleteAtBeginning(struct Node* head) { if (head == NULL) return NULL; if (head->next == head) { // only one node free(head); return NULL; } struct Node* last = head; while (last->next != head) { // find last node last = last->next; } struct Node* temp = head; head = head->next; last->next = head; // last node now points to new head free(temp); return head; }
Time Complexity: O(n) — finding the last node requires a full traversal.
5.2 Deletion at End
struct Node* deleteAtEnd(struct Node* head) { if (head == NULL) return NULL; if (head->next == head) { free(head); return NULL; } struct Node* secondLast = head; while (secondLast->next->next != head) { // stop at second-last node secondLast = secondLast->next; } struct Node* last = secondLast->next; secondLast->next = head; free(last); return head; }
Time Complexity: O(n) — traversal is required to reach the second-last node.
5.3 Deletion at Middle (at a given position)
struct Node* deleteAtPosition(struct Node* head, int pos) { if (pos == 0) return deleteAtBeginning(head);
struct Node* temp = head; for (int i = 0; i < pos - 1; i++) { temp = temp->next; } struct Node* toDelete = temp->next; temp->next = toDelete->next; free(toDelete); return head; }
Time Complexity: O(n) worst case, due to traversal needed to reach the position.
7. Circular Doubly Linked List — Structure and Key
Operations
A Circular Doubly Linked List combines the wrap-around property of a circular list with the bidirectional
pointers of a doubly linked list. The last node's next points to HEAD, and HEAD's prev points to the last
node — so both directions loop continuously.
7.1 Key Advantage Over Circular Singly Linked List
Because HEAD's prev pointer directly gives the address of the last node, insertion at the beginning no
longer requires traversing the entire list to find the last node — it can be done in O(1). This solves the
exact O(n) limitation described earlier for the Circular Singly Linked List.
7.2 Insertion at Beginning (Circular Doubly Linked List)
struct Node* insertAtBeginning(struct Node* head, int value) { struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); newNode->data = value;
if (head == NULL) { newNode->next = newNode; newNode->prev = newNode; return newNode; }
struct Node* last = head->prev; // O(1) access to last node! newNode->next = head; newNode->prev = last; last->next = newNode; head->prev = newNode; head = newNode; return head; }
Time Complexity: O(1) — the last node is reached instantly via head->prev, with no traversal needed.
7.3 Traversal (Forward)
void traverse(struct Node* head) { if (head == NULL) return; struct Node* temp = head; do { printf("%d -> ", temp->data); temp = temp->next; } while (temp != head); printf("(back to head)\n"); }
Time Complexity: O(n), same loop-detection principle as the singly circular case.
8. Real-World Use Cases
8.1 Round-Robin CPU Scheduling
In operating systems, the Round Robin scheduling algorithm allocates a fixed time slice (called a time
quantum) to each process in a queue. After a process's time slice ends, the CPU moves to the next
process, and after the last process in the queue, it wraps back around to the first one. This cyclic
behavior is conceptually implemented using a circular queue structure.
8.2 Turn-Based Multiplayer Games
In games where players take turns in a fixed order (such as board games or card games with more than
two players), a circular linked list is a natural way to represent 'whose turn is next' — moving to the next
node always gives the next player, and after the last player, control cycles back to the first.
8.3 The Josephus Problem
The Josephus Problem is a classic scenario: n people stand in a circle, and starting from a fixed point,
every k-th person is eliminated until only one remains. This problem is commonly solved and taught
using a circular linked list, since eliminating the k-th node while continuing to count around the circle
maps directly onto circular list traversal and deletion.
Diagram: The Josephus Problem — a circle of 5 players
Players 1-5 in a circle; every 2nd player is eliminated (classic Josephus Problem)
Figure 5: Players 1 through 5 arranged in a circular list; the elimination process repeatedly counts around the loop and
removes nodes.
8.4 Circular Buffers
A circular buffer is a fixed-size buffer that, when full, wraps around and starts overwriting from the
beginning again — commonly used in streaming audio/video buffering and producer-consumer systems.
The conceptual 'wrap-around' behavior is identical to a circular linked list, although circular buffers in
practice are often implemented using arrays with modular index arithmetic rather than linked nodes.
10. Solved Examples
Solved Example 1: Trace an insertion at the end
Problem: Given the circular list HEAD → 5 → 15 → 25 → (back to 5), insert 35 at the end. Show the
final next pointer of every node.
Step-by-step solution:
- Traverse from HEAD (5) until last->next == head: 5->next=15, 15->next=25, 25->next=5 (back to
head) → last = node 25.
- Set last->next (node 25's next) = new node 35.
- Set newNode (35)->next = head (node 5).
Final Answer: 5->next=15, 15->next=25, 25->next=35, 35->next=5 (back to HEAD). Full list: HEAD → 5
→ 15 → 25 → 35 → (back to 5).
Solved Example 2: Detect why a traversal loop would fail
Problem: A student writes: while (temp != NULL) { print(temp->data); temp = temp->next; } to traverse a
circular linked list of 4 nodes. Explain what happens when this code runs.
Step-by-step reasoning:
- In a circular linked list, no node's next pointer is ever NULL — the last node's next points back to
HEAD.
- The condition (temp != NULL) is therefore always true, since temp will keep cycling through the
same 4 nodes forever.
- This creates an infinite loop that never terminates on its own.
Final Answer: The code enters an infinite loop, printing 10 -> 20 -> 30 -> 40 -> 10 -> 20 -> ... forever.
The correct fix is to use 'while (temp != head)' or a do-while loop that checks against HEAD, not NULL.
Solved Example 3: Deletion at the beginning — pointer trace
Problem: Given HEAD → 8 → 16 → 24 → (back to 8), delete the node at the beginning (node 8).
Step-by-step solution:
- Traverse to find the last node: last = node 24 (since 24->next == head, which is node 8).
- Save temp = head (node 8, to be deleted).
- Update head = head->next = node 16.
- Update last->next (node 24's next) = new head (node 16).
- Free node 8.
Final Answer: HEAD → 16 → 24 → (back to 16).
Solved Example 4: Josephus-style elimination trace (small case)
Problem: Three players (1, 2, 3) stand in a circle. Starting the count at player 1, every 2nd player is
eliminated (i.e., count 1, 2 and remove the player the count lands on), continuing around the circle until
only one player remains. Who survives?
Step-by-step solution:
- Circle: 1^ →^2 →^3 →^ (back to 1). Start counting at player 1.
- Count 1 = player 1, count 2 = player 2^ →^ player 2 is eliminated. Remaining circle: 1^ →^3 →^ (back
to 1).
- Continue counting from player 3 (the next player after the eliminated one): count 1 = player 3,
count 2 = player 1 → player 1 is eliminated. Remaining: only player 3.
Final Answer: Player 3 survives.
Solved Example 5: Choosing singly vs doubly circular for a task
Problem: A music player needs to repeatedly cycle through a playlist in a loop, and the user frequently
presses both 'Next' and 'Previous.' Which structure is more efficient — Circular Singly or Circular Doubly
Linked List?
Reasoning:
- 'Next' requires moving forward — supported directly by the next pointer in both structures.
- 'Previous' requires moving backward. A Circular Singly Linked List has no prev pointer, so
moving backward would require traversing almost the entire circle (O(n)) to find the preceding
node.
- A Circular Doubly Linked List supports moving backward in O(1) via the prev pointer.
Final Answer: A Circular Doubly Linked List is more efficient here, since it supports both 'Next' and
'Previous' in O(1), matching exactly what the music player needs.
Quick Revision Box
Circular Linked List: Last node's next points back to HEAD instead of NULL — no NULL pointer
exists in a non-empty circular list. Types: Circular Singly (one pointer, one-directional loop) and
Circular Doubly (two pointers, bidirectional loop). Traversal: must detect return to HEAD (do-while
loop), never check against NULL. Key accuracy point: insertion/deletion at the beginning is O(n) for
Circular Singly (must find the last node) but O(1) for Circular Doubly (last node available instantly via
head->prev). Real-world uses: round-robin scheduling, turn-based games, the Josephus Problem,
and circular buffers.
— End of Notes —