💡 Summary: Like a Ferris Wheel or "Repeat All" Playlist That Never Hits a Dead End
In a regular linked list, the last node points to NULL/None—so once you reach the end, you have to go back to the start to keep going. A circular linked list connects the end back to the beginning so you can keep looping around: in a circular singly linked list, the last node's next points back to head; in a circular doubly linked list, the last node points forward to head and head points backward to the last node.
Because there is no NULL at the end, your loop has to watch for when you come full circle back to where you started—otherwise it can spin forever!
- Circular Singly Linked List: Each node has
dataandnext, and the last node'snextloops back tohead(if there's only one node, it points to itself). - Circular Doubly Linked List: Each node has
data,prev, andnext, letting you step forward past the end intoheador backward beforeheadinto the tail. - How to Stop Traversing: Because there's no
nullptrat the end, start athead, take at least one step, and stop as soon as you come back tohead.
| What You Are Comparing | Circular Singly List | Circular Doubly List |
|---|---|---|
| Pointers in Each Node | Only next |
Both prev and next |
| How You Loop Around | One way around the circle (tail → head) | Both ways around the circle (tail ↔ head) |
| Stepping Backward | Have to walk around the loop to reach the previous node | Can step straight back with node->prev |
#include
using namespace std;
struct CNode {
int data;
CNode* next;
CNode(int val) : data(val), next(nullptr) {}
};
void printCircular(CNode* head) {
if (head == nullptr) {
cout << "Empty\n";
return;
}
CNode* curr = head;
do {
cout << curr->data << " -> ";
curr = curr->next;
} while (curr != head);
cout << "(back to " << head->data << ")\n";
}
int main() {
CNode* n1 = new CNode(10);
CNode* n2 = new CNode(20);
CNode* n3 = new CNode(30);
n1->next = n2;
n2->next = n3;
n3->next = n1; // Connect the last node back to the first node
printCircular(n1);
// Break the circle before freeing memory so the cleanup loop ends cleanly
n3->next = nullptr;
CNode* curr = n1;
while (curr != nullptr) {
CNode* nxt = curr->next;
delete curr;
curr = nxt;
}
return 0;
}
tail pointer (where tail->next is always the front/head), adding items at the end or front is quick because you know where the loop closes:
- Adding to an Empty Circle: Point the new node's
nextto itself and settailto that node. - Adding to a Non-Empty Circle: Set
node.next = tail.next(so it points to the current front), settail.next = node, and movetail = nodewhen appending at the back. - Be Careful When Removing Nodes: If you delete the very last node in the list, the list becomes empty (
tail = None/nullptr)—not a self-loop!
class CNode:
def __init__(self, data, nxt=None):
self.data = data
self.next = nxt
class CircularSinglyList:
def __init__(self):
self.tail = None # self.tail.next points to the first node
def append(self, val):
node = CNode(val)
if self.tail is None:
node.next = node
self.tail = node
return
node.next = self.tail.next
self.tail.next = node
self.tail = node
def values(self):
if self.tail is None:
return []
head = self.tail.next
out = []
curr = head
while True:
out.append(curr.data)
curr = curr.next
if curr is head:
break
return out
cll = CircularSinglyList()
for x in [1, 2, 3, 4]:
cll.append(x)
print("Circular elements:", cll.values())
- 1. Taking Turns in a Loop (Round-Robin): Processes or tasks each get a short turn, and after the last one goes, the turn wraps back to the first one.
- 2. "Repeat All" on a Music or Video Playlist: After the last song finishes, it starts over from the first song automatically.
- 3. Player Turns in a Board or Card Game: Play goes around the table from player to player and loops back to Player 1 after the last player.
- 4. Lobby / Store Display Slideshow: Promo screens or announcements cycle endlessly—and two-way circular links help if someone steps back or forward.
- 5. Ring / Wrap-Around Buffers: When a fixed-size buffer wraps from the last slot back to slot
0using modulo (%), it behaves like a circle.
# 1. Taking turns across 3 processes for 6 time slices
processes = ["P1", "P2", "P3"]
n = len(processes)
schedule = [processes[tick % n] for tick in range(6)]
print("1. Round-robin slices:", schedule)
# 2. Wrapping from the last track back to the first track
playlist = ["Intro", "Verse", "Chorus"]
idx = 2
next_idx = (idx + 1) % len(playlist)
print("2. Next track after last:", playlist[next_idx])
- 6. Rotating Weekly Duty or On-Call Roster: Team members take turns week after week and loop back to the start of the list.
- 7. Traffic Light Sequence: Signals repeat in a fixed cycle (like Green → Yellow → Red → Green).
- 8. Cycling Through Tabs or Open Windows: Pressing a shortcut past the last item wraps around to the first item.
- 9. Counting Around a Circle (Josephus-Style Games): Stepping a fixed count around a ring and eliminating someone each round is a classic circular-list problem.
- 10. Checking Devices One After Another in a Loop: A controller checks a ring of sensors or ports repeatedly.
# 6. Rotating weekly on-call duty around a 4-person team
team = ["Ananya", "Dev", "Farhan", "Kiran"]
for week in range(1, 6):
on_call = team[(week - 1) % len(team)]
print(f"Week {week} on-call: {on_call}")
- Don't write
while curr != nullptr:for a circular list: There is nonullptrat the end, so that condition would loop forever! Check whencurrreturns toheadinstead. - Test empty and single-node cases: Make sure your code handles an empty list and a list with just one node looping back to itself.
- Know when to use a circular list: Pick it when items naturally repeat in a cycle without a fixed "end" point.
cll_tips = [
"No NULL at the end: the last node points back to the start.",
"Stop walking once you return to the starting node so you don't loop forever.",
"Circular doubly lists let you loop both forward and backward easily."
]
for tip in cll_tips:
print("*", tip)