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

জাভাস্ক্রিপ্টে কিউ (Queue) ডেটা স্ট্রাকচার তৈরি করা


Queue Class Implementation in JavaScript

যদিও JavaScript অ্যারে নেটিভলি কিউর সব ফাংশনালিটি দেয় (push, shift), কাস্টম ক্লাস বানানো কন্ট্রোল, ক্যাপাসিটি লিমিট, এবং ডেটা স্ট্রাকচার বোঝার জন্য উপকারী।

সম্পূর্ণ Queue Class

class Queue {
  constructor(maxSize = 10) {
    // ডিফল্ট ম্যাক্স সাইজ ১০
    this.maxSize = maxSize;
    // ইন্টারন্যাল স্টোরেজ
    this.container = [];
  }

  // কন্টেন্টস ডিসপ্লে (ডিবাগিংয়)
  display() {
    console.log(this.container);
  }

  // কিউ খালি কিনা
  isEmpty() {
    return this.container.length === 0;
  }

  // কিউ ফুল কিনা
  isFull() {
    return this.container.length >= this.maxSize;
  }

  // এলিমেন্ট যোগ করুন (টেইলে)
  enqueue(element) {
    if (this.isFull()) {
      console.log("Queue Overflow!");
      return false;
    }
    this.container.push(element);
    return true;
  }

  // এলিমেন্ট সরান (হেড থেকে)
  dequeue() {
    if (this.isEmpty()) {
      console.log("Queue Underflow!");
      return undefined;
    }
    return this.container.shift();
  }

  // হেড এলিমেন্ট দেখুন (রিমুভ ছাড়া)
  peek() {
    if (this.isEmpty()) {
      console.log("Queue Underflow!");
      return undefined;
    }
    return this.container[0];
  }

  // কিউ খালি করুন
  clear() {
    this.container = [];
  }

  // বর্তমান সাইজ
  size() {
    return this.container.length;
  }
}

ব্যবহারের উদাহরণ

const q = new Queue(3);

console.log("Empty?", q.isEmpty()); // true

q.enqueue("Task 1");
q.enqueue("Task 2");
q.enqueue("Task 3");
q.display(); // [ 'Task 1', 'Task 2', 'Task 3' ]

console.log("Size:", q.size()); // 3
console.log("Peek:", q.peek()); // 'Task 1'

console.log("Dequeue:", q.dequeue()); // 'Task 1'
q.display(); // [ 'Task 2', 'Task 3' ]

q.clear();
q.display(); // []
console.log("Empty after clear?", q.isEmpty()); // true

হেল্পার ফাংশনSONের ভূমিকা

  • isEmpty(): ডিকিউ/পিক করার আগে চেক করে Underflow এড়াতে।
  • isFull(): এনকিউ করার আগে চেক করে Overflow এড়াতে।
  • display(): ডেভেলপমেন্ট/ডিবাগিং-এ কন্টেন্ট দেখতে সাহায্য করে।

এক্সটেনশন আইডিয়া

  • Priority Queue: প্রায়োরিটি দিয়ে সর্টেড ইনসারশন।
  • Circular Queue: ফিক্সড সাইজ অ্যারে + মডুলো أرিথমেটিক (O(1) ডিকিউ)।
  • Deque (Double-ended Queue): উভয় প্রান্তে ইনসার্ট/ডিলিট।
  • Observable Queue: ইভেন্ট ইমিট (onEnqueue, onDequeue) ।

বাস্তব ব্যবহার: Async Task Queue

class TaskQueue extends Queue {
  constructor(concurrency = 1) {
    super(Infinity); // আনলিমিটেড ক্যাপাসিটি
    this.concurrency = concurrency;
    this.running = 0;
  }

  async enqueue(task) {
    super.enqueue(task);
    this.process();
  }

  async process() {
    while (this.running < this.concurrency && !this.isEmpty()) {
      this.running++;
      const task = this.dequeue();
      try {
        await task();
      } finally {
        this.running--;
        this.process();
      }
    }
  }
}

// ব্যবহার
const taskQueue = new TaskQueue(2); // একসাথে ২ টা টাস্ক
taskQueue.enqueue(() => fetch('/api/1'));
taskQueue.enqueue(() => fetch('/api/2'));
taskQueue.enqueue(() => fetch('/api/3'));
  1. জাভাস্ক্রিপ্টে কিউ (Queue) ডেটা স্ট্রাকচার: কনসেপ্ট ও বেসিক

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

  3. জাভাস্ক্রিপ্ট দিয়ে কাস্টম ইমেজ স্লাইডার তৈরি করা

  4. জাভাস্ক্রিপ্টে কিউ (Queue) বাস্তবায়ন