Circular linked list

Comprehensive study notes, diagrams, and exam preparation for Circular linked list.

Circular Linked List

Definition

A circular linked list is a linked list in which the last node’s link field points to the first node, creating a circular chain of nodes instead of ending with a null reference.

In a circular linked list:

  • Every node contains data and a pointer/link to the next node.
  • The last node links back to the head node.
  • Traversal from any node can continue around the circle until the starting node is encountered again.

If head is the first node and tail is the last node, then:

tail->next = head

This is the defining property of a circular linked list.


Main Content

1. First Concept: Structure of a Circular Linked List

  • A circular linked list is built from nodes, and each node contains:
  • Data part: stores the actual value
  • Link part: stores the address of the next node
  • Unlike a simple linear linked list, the last node points back to the first node, creating a closed chain.
  • Because of this circular connection, there is no NULL at the end of the list.
  • A circular linked list may be:
  • Singly circular linked list: each node points to the next node, and the last node points to the first node
  • Doubly circular linked list: each node has both next and prev pointers, and the first and last nodes are connected in both directions

Example of a singly circular linked list:

[5] -> [12] -> [18] -> [25]
 ^                       |
 |_______________________|

This means:

  • 5 is the first node
  • 25 is the last node
  • 25 points back to 5

2. Second Concept: Traversal in a Circular Linked List

  • Traversal means visiting each node exactly once in a proper order.
  • In a circular linked list, traversal cannot rely on NULL as the stopping condition because the list does not end with NULL.
  • Instead, traversal usually starts at the head node and continues until the traversal reaches the head again.
  • A common method is:
  • Print/process the first node
  • Move to the next node
  • Keep moving until the current node becomes equal to the starting node again
  • Care must be taken to avoid an infinite loop

Example traversal flow:

Start at head
   ↓
Node 1 → Node 2 → Node 3 → Node 4
   ↑                         ↓
   └─────────────────────────┘

If the traversal starts at Node 1, it should stop when Node 1 is encountered again after Node 4.

Important observation:

  • Circular traversal is useful when repeated cycling through the same nodes is needed.
  • It is commonly used in scheduling tasks where each process gets a turn one after another.

3. Third Concept: Insertion and Deletion Operations

Insertion

  • means adding a new node to the circular linked list.

Deletion

  • means removing an existing node from the list.
  • These operations require updating pointers carefully so the circular structure remains valid.

Insertion cases:

Insert at beginning

  • New node points to the current head
  • Last node points to the new node
  • Head is updated to the new node

Insert at end

  • New node points to the head
  • Old last node points to the new node

Insert in middle

  • Adjust links of neighboring nodes so the new node fits between them

Deletion cases:

Delete from beginning

  • Head moves to the next node
  • Last node’s link is updated to the new head

Delete from end

  • Find the second-last node
  • Make it point to the head

Delete a specific node

  • Search for the node
  • Bypass it by changing the previous node’s link

Example of inserting 15 between 10 and 20:

Before:
[10] -> [20] -> [30]
 ^                 |
 |_________________|

After:
[10] -> [15] -> [20] -> [30]
 ^                           |
 |___________________________|

Correct pointer handling is essential; otherwise, the circular chain may break or form a wrong loop.


Working / Process

1. Create the first node

  • Allocate memory for the first node.
  • Store data in it.
  • Make its next pointer refer to itself if it is the only node in the list.
  • This self-reference is a valid circular form for a single-node circular linked list.

2. Add more nodes

  • For each new node, allocate memory and store the data.
  • Link the new node to the first node or next appropriate node.
  • Update the last node so that its pointer always refers back to the head node.

3. Traverse or modify the list

  • Start from the head node.
  • Visit each node in order.
  • Stop when the starting node is reached again.
  • For insertion or deletion, carefully change the links so the circle remains complete.

Example of a single-node circular list:

[40]
  |
  └── points to itself

Example of a three-node circular list:

[10] -> [20] -> [30]
 ^                 |
 |_________________|

This process ensures the structure remains continuous and usable for repeated cycles.


Advantages / Applications

Efficient cyclic traversal

  • Ideal when the data must be visited repeatedly in a loop.
  • Useful in applications like process scheduling, where each task gets repeated turns.

No need to restart from the beginning

  • Since the last node connects to the first node, traversal can continue naturally without special reset logic.
  • This makes implementation convenient in circular queues and round-robin systems.

Useful in real-world systems

  • Commonly used in:
    • Round-robin CPU scheduling
    • Circular buffers
    • Multiplayer games and turn-based applications
    • Repeating playlists
    • Network token passing
  • Also helpful when data needs to be maintained in a continuous cycle.

Summary

  • Circular linked list is a linked list in which the last node points back to the first node.
  • It supports continuous traversal without a null ending.
  • It is especially useful for repeated or cyclic processing tasks.
  • Important terms to remember
  • Node
  • Head
  • Tail
  • Link pointer
  • Traversal
  • Insertion
  • Deletion
  • Singly circular linked list
  • Doubly circular linked list