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

জাভাস্ক্রিপ্টে লিংকড লিস্ট থেকে এলিমেন্ট রিমুভ করা


Singly Linked List: Remove Operation (এলিমেন্ট মুছে ফেলা)

লিংকড লিস্টে এলিমেন্ট রিমুভ মানে ঐ নোডের রেফারেন্স গুমাতে হয় যাতে এটি লিস্ট থেকে আলাদা হয়। তিনটি প্রধান কেস রয়েছে।

৩টি রিমুভাল কেস

  1. হেড থেকে রিমুভ (Position 0): head = head.next — পুরানো হেড রেফারেন্স হারায়, GC ক্লিন আপ করে।
  2. টেইল থেকে রিমুভ (Last Position): সেকেন্ড-লাস্ট নোড খুঁজে তার next = null।
  3. মাঝপথ থেকে রিমুভ: টার্গেট নোডের আগের নোড খুঁজে prev.next = target.next।

Remove মেথড ইমপ্লিমেন্টেশন

class LinkedList {
  constructor() {
    this.head = null;
    this.length = 0;
  }

  // ... insert, display methods ...

  remove(data, position = 0) {
    if (this.length === 0) {
      console.log("List is already empty");
      return;
    }
    this.length--;

    let currNode = this.head;

    // Case 1: Remove Head (Position <= 0)
    if (position <= 0) {
      this.head = this.head.next;
    }
    // Case 2: Remove Tail (Position >= Length - 1)
    else if (position >= this.length) {
      // Traverse to Second Last Node
      while (currNode.next.next !== null) {
        currNode = currNode.next;
      }
      currNode.next = null; // Last node detached
    }
    // Case 3: Remove Middle
    else {
      let iter = 0;
      while (iter < position) {
        currNode = currNode.next;
        iter++;
      }
      // Skip the target node
      currNode.next = currNode.next.next;
    }
    return currNode;
  }
}

LinkedList.prototype.Node = class {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
};

টেস্টিং উদাহরণ

const list = new LinkedList();

list.insert(10);  // 10
list.insert(20);  // 10 -> 20
list.insert(30);  // 10 -> 20 -> 30

console.log("Original:");
list.display(); // 10 -> 20 -> 30 -> null

list.remove(1);   // Remove at position 1 (20)
console.log("After remove(1):");
list.display(); // 10 -> 30 -> null

list.insert(15, 2); // Insert 15 at position 2 (end)
list.remove();      // Remove head (default position 0)
console.log("After insert + remove head:");
list.display(); // 30 -> 15 -> null

আউটপুট ব্যাখ্যা

Original:
10 -> 20 -> 30 -> null
After remove(1):
10 -> 30 -> null
After insert + remove head:
30 -> 15 -> null

নোট: আউটপুটে 30 -> 15 -> দেখা যাচ্ছে কারণ: ১০, ২০, ৩০ -> রিমুভ(১) -> ১০, ৩০ -> ইনসার্ট(১৫, পজিশন ২) -> ১০, ৩০, ১৫ -> রিমুভ(হেড) -> ৩০, ১৫।

ভিজ্যুয়াল: Remove Middle Node

BEFORE:  HEAD → [10] → [20] → [30] → null
                    ↑
              Remove Position 1 (Node 20)

STEP: currNode = Node(10) [iter=0, position=1]
      currNode.next = currNode.next.next
      // Node(10).next = Node(30)

AFTER:   HEAD → [10] ──────→ [30] → null
              ↑              ↑
           (next)         (prev ref lost, GC collects)

Edge Cases & Improvements

  • Invalid Position: Negative বা > length হ্যান্ডল করা উচিত।
  • Return Value: রিমুভড নোডের ডেটা রিটার্ন করলে বেশি ইউজফুল।
  • Memory: JS-এ গারবেজ কলেক্টর অটোমেটিকলি ডিট্যাচড নোড ক্লিন করে।
  • Doubly Linked List-এ: node.next.prev = node.prev ও আপডেট লাগে।
  1. জাভাস্ক্রিপ্টে লিংকড লিস্টে এলিমেন্ট যোগ করা (Insert)

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

  3. জাভাস্ক্রিপ্টে লিংকড লিস্ট রিপ্রেজেন্টেশন (Visual)

  4. জাভাস্ক্রিপ্ট ব্যবহার করে ডাবল লিংকড লিস্ট থেকে এলিমেন্ট সরানো