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

জাভাস্ক্রিপ্টে Queue ক্লাসের সম্পূর্ণ ইমপ্লিমেন্টেশন


Queue Class: সম্পূর্ণ ইমপ্লিমেন্টেশন (FIFO)

এখানে একটি প্রোডাকশন-রেডি Queue ক্লাস দেওয়া হলো যেটিতে সব প্রয়োজনীয় মেথড এবং এরর হ্যান্ডলিং অন্তর্ভুক্ত।

সম্পূর্ণ Queue Class

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

  // Helper: Display contents (Debugging)
  display() {
    console.log(this.container);
  }

  // Check if empty
  isEmpty() {
    return this.container.length === 0;
  }

  // Check if full
  isFull() {
    return this.container.length >= this.maxSize;
  }

  // Enqueue: Add to Rear (Tail)
  enqueue(element) {
    if (this.isFull()) {
      console.log("Queue Overflow! কিউর সর্বোচ্চ সাইজে পৌঁছেছে।");
      return false;
    }
    this.container.push(element);
    return true;
  }

  // Dequeue: Remove from Front (Head)
  dequeue() {
    if (this.isEmpty()) {
      console.log("Queue Underflow! কিউ খালি।");
      return undefined;
    }
    return this.container.shift();
  }

  // Peek: View Front without removing
  peek() {
    if (this.isEmpty()) {
      console.log("Queue Underflow! কিউ খালি।");
      return undefined;
    }
    return this.container[0];
  }

  // Clear all elements
  clear() {
    this.container = [];
  }

  // Current size
  size() {
    return this.container.length;
  }
}

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

const q = new Queue(3);

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

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

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

q.clear();
q.display(); // []

Time Complexity (Array-based)

Operation Complexity Note
enqueue O(1) push() — Amortized O(1)
dequeue O(n) shift() — সব এলিমেন্ট রিইন্ডেক্স
peek O(1) container[0]
isEmpty / isFull / size / clear O(1) —

Optimization: Circular Queue for O(1) Dequeue

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;
    this.head = (this.head + 1) % this.capacity;
    this.size--;
    return item;
  }

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

  clear() {
    this.queue = new Array(this.capacity);
    this.head = 0;
    this.tail = 0;
    this.size = 0;
  }
}

বাস্তব ব্যবহার: BFS (Breadth-First Search)

function bfs(graph, startNode) {
  const visited = new Set();
  const queue = new Queue();
  const result = [];

  queue.enqueue(startNode);
  visited.add(startNode);

  while (!queue.isEmpty()) {
    const current = queue.dequeue();
    result.push(current);

    for (const neighbor of graph[current] || []) {
      if (!visited.has(neighbor)) {
        visited.add(neighbor);
        queue.enqueue(neighbor);
      }
    }
  }
  return result;
}

// Graph: Adjacency List
const graph = {
  'A': ['B', 'C'],
  'B': ['D', 'E'],
  'C': ['F'],
  'D': [], 'E': ['F'], 'F': []
};

console.log(bfs(graph, 'A')); // [ 'A', 'B', 'C', 'D', 'E', 'F' ]
  1. জাভাস্ক্রিপ্টে কিউ (Queue) ডেটা স্ট্রাকচার: কনসেপ্ট ও বেসিক

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

  3. C#-তে Queue ক্লাস কী?

  4. C#-এ Object ক্লাস: বেস ক্লাস এবং এর মেথডসমূহ