Queue Visualizer

What is a Queue ?

A Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. Elements are added at the rear (enqueue) and removed from the front (dequeue). It operates much like a real-world queue (line) where the first person to arrive is the first to be served.

Enqueue Operation

Enqueue adds an element to the end (rear) of the queue.

Example with queue:[10, 20, 30]
  1. Before Enqueue: [10, 20, 30]
  2. Enqueue(40): Add 40 to the rear
  3. After Enqueue: [10, 20, 30, 40]

The new element always goes to the end of the queue.

Dequeue Operation

Dequeue removes and returns the element from the front (head) of the queue.

Example with queue:[10, 20, 30, 40]
  1. Before Dequeue: [10, 20, 30, 40]
  2. Dequeue(): Remove and return 10
  3. After Dequeue: [20, 30, 40]

The oldest element (first one added) is always removed first.

Algorithm Steps for Enqueue

  • Check if the queue is full (in case of fixed-size implementation)
  • If full, return overflow error (or resize in dynamic implementation)
  • Increment the rear pointer
  • Add the new element at the rear position

Algorithm Steps for Dequeue

  • Check if the queue is empty
  • If empty, return underflow error
  • Access the data at the front of the queue
  • Increment the front pointer to the next element
  • Return the accessed data

Time Complexity

  1. Enqueue Operation: O(1) - Constant time to add to the end
  2. Dequeue Operation: O(1) - Constant time to remove from the front
  3. Peek Operation: O(1) - Constant time to examine the front element

Space Complexity

The space complexity is O(n) where n is the number of elements in the queue, as it needs to store all elements.

Queues are fundamental in computer science and are used in various applications like CPU scheduling, disk scheduling, handling interrupts, breadth-first search, and any scenario where you need to maintain order of processing.

Visualize First-In-First-Out (FIFO) operations in real-time

Queue ready

Queue Visualization

Queue is empty - add some elements!

Queue Implementation (Enqueue & Dequeue Only)

// Queue Implementation in JavaScript (Linked List)
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
  }
  
  // Add element to the rear (enqueue)
  enqueue(item) {
    const newNode = new Node(item);
    if (this.rear === null) {
      this.front = this.rear = newNode;
    } else {
      this.rear.next = newNode;
      this.rear = newNode;
    }
  }
  
  // Remove element from front (dequeue)
  dequeue() {
    if (this.front === null) {
      return "Queue Underflow";
    }
    const temp = this.front;
    this.front = temp.next;
    
    if (this.front === null) {
      this.rear = null;
    }
    return temp.data;
  }
}

// Usage Example
const queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log(queue.dequeue()); // 10
console.log(queue.dequeue()); // 20

Explore Other Operations