কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে সিংলি লিংকড লিস্টকে সার্কুলার (Circular) করা


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)。——
  1. জাভাস্ক্রিপ্টে লিংকড লিস্ট রিপ্রেজেন্টেশন (Visual)

  2. জাভাস্ক্রিপ্টে ডাবলি লিংকড লিস্টকে সার্কুলার (Circular) করা

  3. সিঙ্গলি লিংকড লিস্টের সব নোডের গুণফল বের করা

  4. রুবিতে লিঙ্কড লিস্টে দক্ষতা অর্জন: একটি ব্যাবহারিক গাইড