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

জাভাস্ক্রিপ্টে কিউ (Queue) থেকে ডিকিউ (Dequeue) অপারেশন


Queue: Dequeue (Remove from Front) অপারেশন

Dequeue মানে কিউর ফ্রন্ট/হেড (সামনের) এলিমেন্টটি সরিয়ে ফেলা এবং তার মান রিটার্ন করা। আমরা অ্যারের সূচনা (index 0) কে কিউর হেড হিসেবে ধরে কাজ করছি।

Dequeue ফাংশন ইমপ্লিমেন্টেশন

class Queue {
  constructor(maxSize = 10) {
    this.maxSize = maxSize;
    this.container = [];
  }

  isEmpty() {
    return this.container.length === 0;
  }

  dequeue() {
    // খালি কিউ চেক
    if (this.isEmpty()) {
      console.log("Queue Underflow! কিউ খালি, ডিকিউ সম্ভব নয়।");
      return undefined;
    }
    // shift() দিয়ে প্রথম এলিমেন্ট সরিয়ে আনে
    return this.container.shift();
  }

  // Helper methods
  enqueue(element) {
    if (this.container.length >= this.maxSize) {
      console.log("Queue Overflow!");
      return;
    }
    this.container.push(element);
  }
  display() { console.log(this.container); }
}

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

const q = new Queue(5);

// খালি কিউ থেকে ডিকিউ চেষ্টা
q.dequeue(); // আউটপুট: "Queue Underflow!..."

// এলিমেন্ট যোগ করুন
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
console.log("Before dequeue:"); 
q.display(); // [ 10, 20, 30 ]

// ডিকিউ করুন
const removed = q.dequeue();
console.log("Removed:", removed); // 10
console.log("After dequeue:");
q.display(); // [ 20, 30 ]

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

Queue Underflow! কিউ খালি, ডিকিউ সম্ভব নয়।
Before dequeue:
[ 10, 20, 30 ]
Removed: 10
After dequeue:
[ 20, 30 ]

১০ প্রথমে কিউতে ঢুকেছিল (FIFO), তাই এটি প্রথমে বের হয়। ২০ এবং ৩০ আছেন।

পারফরম্যান্স নোট

  • Array.prototype.shift() হলো O(n) অপারেশন (সব এলিমেন্ট রিইন্ডেক্স হয়)।
  • বড় কিউর জন্য এটি স্লো হতে পারে।
  • অপ্টিমাইজেশন: Circular buffer বা Linked List ব্যবহার করুন O(1) ডিকিউর জন্য।

বিকল্প ইমপ্লিমেন্টেশন: Circular Queue (O(1))

class CircularQueue {
  constructor(capacity = 10) {
    this.capacity = capacity;
    this.queue = new Array(capacity);
    this.head = 0;
    this.tail = 0;
    this.size = 0;
  }

  isEmpty() { return this.size === 0; }
  isFull() { return this.size === this.capacity; }

  enqueue(item) {
    if (this.isFull()) throw new Error("Queue Overflow");
    this.queue[this.tail] = item;
    this.tail = (this.tail + 1) % this.capacity;
    this.size++;
  }

  dequeue() {
    if (this.isEmpty()) throw new Error("Queue Underflow");
    const item = this.queue[this.head];
    this.queue[this.head] = undefined; // GC کمک
    this.head = (this.head + 1) % this.capacity;
    this.size--;
    return item;
  }

  peek() {
    if (this.isEmpty()) return undefined;
    return this.queue[this.head];
  }
}

সারাংশ

  • dequeue() হেড এলিমেন্ট রিমুভ করে রিটার্ন করে।
  • এম্পটি কিউর জন্য Underflow হ্যান্ডল করুন।
  • অ্যারে-ভিত্তিক ইমপ্লিমেন্টেশনে shift() O(n), Circular Queue O(1)।’
  1. জাভাস্ক্রিপ্টে PriorityQueue-এ Dequeue (Remove) অপারেশন

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

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

  4. জাভাস্ক্রিপ্টে splice() মেথড ব্যবহার করে এলিমেন্ট রিমুভ করার পদ্ধতি