Search⌘ K
AI Features

Key Operations of a Queue

Explore core queue operations including isEmpty, isFull, enqueue, dequeue, peek, and size. Understand their constant time complexity and how to implement them in JavaScript for efficient data handling.

Now that we understand how queues are implemented using arrays and linked lists, let's explore the fundamental operations for interacting with a queue. These operations define what makes a queue useful and how we manipulate the data it holds.

A queue supports several core operations, but they all revolve around the FIFO (First in, first out) principle. Before we can add or remove elements, we often need to check the queue's current state. Let’s begin with two essential utility operations.

The isEmpty operation

The isEmpty operation checks whether the queue contains any elements. This is crucial before attempting to remove or view elements, as attempting to dequeue from an empty queue results in an error.

Example: Consider a queue that currently holds the elements [88, 95, 72, 80, 91]. If we call isEmpty() on this queue, it would return False because the queue’s size is 5, not 0.

If we were to dequeue all five elements one by one, size would eventually become 0 again. At that point, calling isEmpty() would return True.

JavaScript implementation

In an array-based queue, we can check if the queue is empty by comparing the size counter with 0. If size is equal to 0, no elements are present in the queue.

Javascript (babel-node)
class Queue {
constructor(capacity = 10) {
this.capacity = capacity; // Maximum size of the queue
this.data = new Array(capacity).fill(null); // Array to store elements
this.front = 0; // Index of the front element
this.rear = -1; // Index of the rear element
this.size = 0; // Number of elements currently in the queue
}
isEmpty() {
/** Check if the queue is empty */
return this.size === 0;
}
}
// Create an empty queue
const queue = new Queue(10);
console.log("Is queue empty?", queue.isEmpty()); // Output: true (queue is empty)

The isFull operation

The isFull operation checks whether the queue has reached its maximum capacity. This is only relevant for array-based queues with a fixed size, as in linked lists, we can add as many elements as we want until the system's memory runs out.

Example: Consider a queue with capacity 5 that currently holds [88, 95, 72, 80, 91]. If we call the isFull() function, it would return True.

If we dequeue one element (88), size would become 4. Now size (4) is less than capacity (5), so calling isFull() would return False.

JavaScript implementation

In an array-based queue, we check if size equals capacity. If they match, the queue has no room for additional elements.

Javascript (babel-node)
class Queue {
constructor(capacity = 10) {
this.capacity = capacity; // Maximum size of the queue
this.data = new Array(capacity).fill(null); // Array to store elements
this.front = 0; // Index of the front element
this.rear = -1; // Index of the rear element
this.size = 0; // Number of elements currently in the queue
}
isEmpty() {
/** Check if the queue is empty */
return this.size === 0;
}
isFull() {
/** Check if the queue is full */
return this.size === this.capacity;
}
}
// Create a queue with capacity 5
const queue = new Queue(5);
// Simulate enqueuing 5 elements: [88, 95, 72, 80, 91]
queue.data[0] = 88;
queue.data[1] = 95;
queue.data[2] = 72;
queue.data[3] = 80;
queue.data[4] = 91;
queue.front = 0;
queue.rear = 4;
queue.size = 5; // Set size to 5 (full)
console.log("Is queue full?", queue.isFull()); // Output: true (queue is full, size=5, capacity=5)

Enqueue operation

The enqueue operation adds a new element to the rear of the queue. This is how we insert data into the queue, and it always happens at the rear end. ...