Singly Circular Linked List: কনসেপ্ট
Singly Circular Linked List-এ শুধু next পয়েন্টার থাকে, কিন্তু শেষ নোডের next প্রথম নোডকে (হেড) পয়েন্ট করে। এটি একটি লুপ তৈরি করে।
স্ট্রাকচার ভিজ্যুয়াল
HEAD (Node 1)
↓
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Data: 1 │───▶│ Data: 2 │───▶│ Data: 3 │
│ Next: ──┼────▶│ Next: ──┼────▶│ Next: ──┼──┐
└─────────┘ └─────────┘ └─────────┘ │
↑ │
└─────────────────────────────────┘
(Tail.next = Head)
বেস্টি সিংলি লিস্ট থেকে পার্থক্য
| Feature | Singly Linked List | Singly Circular Linked List |
|---|---|---|
| Tail.next | null | Head |
| Traversal End Condition | curr !== null |
curr !== head (do-while) |
| Access Tail from Head | O(n) | O(n) — but can loop |
| Insert at End (w/o Tail Ref) | O(n) | O(n) — same |
বেসিক অপারেশন (স্নিপেট)
class CircularSinglyLinkedList {
constructor() {
this.head = null;
this.length = 0;
}
// Append at End
append(data) {
const node = new Node(data);
if (!this.head) {
this.head = node;
node.next = node; // Self-loop
} else {
// Find Tail (node.next === head)
let tail = this.head;
while (tail.next !== this.head) {
tail = tail.next;
}
tail.next = node;
node.next = this.head;
}
this.length++;
}
// Display (do-while loop)
display() {
if (!this.head) return;
let curr = this.head;
let output = "";
do {
output += curr.data + " -> ";
curr = curr.next;
} while (curr !== this.head);
console.log(output + "(head)");
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
Use Cases
- Round-Robin Scheduling (Single Direction)。——
- Simple Carousel/Slider (Next Only)。——
- Resource Pooling (Reuse Objects in Cycle)。——