MODULE 3.3: CIRCULAR SINGLY & DOUBLY LINKED LISTS

A student-friendly guide to circular linked lists—how the end loops back to the start, how to walk around a circle safely, and where loops show up in real software.

💡 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!

1. Circular Singly vs. Circular Doubly Linked Lists
Both versions wrap around into a loop, just with one-way or two-way links:
  • Circular Singly Linked List: Each node has data and next, and the last node's next loops back to head (if there's only one node, it points to itself).
  • Circular Doubly Linked List: Each node has data, prev, and next, letting you step forward past the end into head or backward before head into the tail.
  • How to Stop Traversing: Because there's no nullptr at the end, start at head, take at least one step, and stop as soon as you come back to head.
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
C++ (circular_singly_list.cpp)
#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;
}
Output: 10 -> 20 -> 30 -> (back to 10)
2. Adding Nodes Using a Tail Pointer
If you keep a 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 next to itself and set tail to that node.
  • Adding to a Non-Empty Circle: Set node.next = tail.next (so it points to the current front), set tail.next = node, and move tail = node when 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!
Python (circular_linked_list_ops.py)
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())
Output: Circular elements: [1, 2, 3, 4]
3. 10 Real-Time Examples (Part 1: Examples 1 to 5)
Here are five real-world situations where going around in a circle makes sense:
  • 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 0 using modulo (%), it behaves like a circle.
Python (realtime_cll_1_to_5.py)
# 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])
Output: 1. Round-robin slices: ['P1', 'P2', 'P3', 'P1', 'P2', 'P3'] 2. Next track after last: Intro
4. 10 Real-Time Examples (Part 2: Examples 6 to 10)
Five more real-world examples of cyclic rotation and wrap-around:
  • 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.
Python (realtime_cll_6_to_10.py)
# 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}")
Output: Week 1 on-call: Ananya Week 2 on-call: Dev Week 3 on-call: Farhan Week 4 on-call: Kiran Week 5 on-call: Ananya
5. Exam & Study Tips for Students
Keep these three study tips in mind for lab tests, exams, and coding practice:
  • Don't write while curr != nullptr: for a circular list: There is no nullptr at the end, so that condition would loop forever! Check when curr returns to head instead.
  • 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.
Python (cll_checklist.py)
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)
Output: * 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.