Circular Linked List
🔄 What is a Circular Linked List?
A Circular Linked List (CLL) is a variation of a linked list where the last node points back to the first node, forming a circular loop.
🧠 Key Points:
-
There is no
NULL
at the end. -
It can be singly or doubly circular.
-
In singly circular, each node has a pointer to the next node only.
-
In doubly circular, each node has pointers to both next and previous nodes.
For this lesson, we’ll focus on Singly Circular Linked List.
🎯 Applications of Circular Linked List
-
Circular queues
-
Round-robin schedulers (e.g., CPU scheduling)
-
Multiplayer games where turn rotation is needed
-
Continuous loop playback of media
📘 Structure of a Node (Singly Circular)
In a CLL:
-
If there is only one node, its
next
points to itself. -
In general, the last node's
next
points to the first node.
✅ Basic Operations in Circular Linked List
-
Insertion at the end
-
Insertion at the beginning
-
Deletion of a node
-
Display all nodes
✅ 1. Insert at the End
🧠 Idea:
-
Create a new node.
-
If list is empty, point new node to itself and make it
head
. -
Otherwise, traverse to the last node (node whose
next
is head), insert the new node after it, and update itsnext
tohead
.
📋 Algorithm:
✅ 2. Insert at the Beginning
🧠 Idea:
-
Create a new node.
-
If list is empty, point new node to itself and set as
head
. -
Else, traverse to last node, make last node point to new node, and new node point to
head
, then updatehead
.
📋 Algorithm:
✅ 3. Delete a Node (By Value)
🧠 Idea:
-
Handle three cases:
-
List is empty.
-
Node to delete is
head
. -
Node to delete is somewhere else.
-
-
If node is found, update previous node's
next
pointer to skip the current node and free memory.
📋 Algorithm:
✅ 4. Display the Circular Linked List
🧠 Idea:
-
Start from head, keep printing until you circle back to head.
📋 Algorithm:
✅ 5. Check if List is Empty
Comments
Post a Comment