Queue

Enqueue & Dequeue

What is a Queue?

A queue works exactly like a line of people waiting: whoever joins first at the back gets served first at the front. In data-structure terms, new items go in at the rear through enqueue, and items come out from the front through dequeue, first in, first out.

Enqueue Operation

Enqueue adds an element to the end (rear) of the queue. The front pointer never moves, and the new element becomes the new rear.

frontrear102030

↓ enqueue(40) ↓

frontrear10203040

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. The rear pointer never moves, and whichever element was second in line becomes the new front.

frontrear10203040

↓ dequeue() → returns 10 ↓

frontrear203040

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

Algorithm Steps for Enqueue

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

Algorithm Steps for Dequeue

  1. Check if the queue is empty
  2. If empty, return underflow error
  3. Access the data at the front of the queue
  4. Increment the front pointer to the next element
  5. Return the accessed data

Time Complexity

  • Enqueue Operation: O(1) - Constant time to add to the end
  • Dequeue Operation: O(1) - Constant time to remove from the front

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

Test Your Knowledge before moving forward!

Queue Quiz Challenge

How it works:

  • +1 point for each correct answer
  • 0 points for wrong answers
  • -0.5 point penalty for viewing explanations
  • Earn stars based on your final score (max 5 stars)

Queue (Enqueue & Dequeue)

// 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

Done With the Learning

Mark queue : enqueue & dequeue as done and view it on your dashboard

Explore Other Operations