Search⌘ K
AI Features

Key Operations of a Queue

Explore the fundamental queue operations such as enqueue, dequeue, peek, isEmpty, isFull, and size. Understand how to implement these efficiently in Java and why each operation runs in constant time. This lesson equips you to manipulate queues following the FIFO principle effectively.

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.

Java 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.

Java 25
class Queue {
private int capacity; // Maximum size of the queue
private int[] data; // Array to store elements
private int front; // Index of the front element
private int rear; // Index of the rear element
private int size; // Number of elements currently in the queue
public Queue(int capacity) {
this.capacity = capacity;
this.data = new int[capacity];
this.front = 0;
this.rear = -1;
this.size = 0;
}
public Queue() {
this(10);
}
public boolean isEmpty() {
// Check if the queue is empty
return size == 0;
}
}
public class Main {
public static void main(String[] args) {
Queue queue = new Queue(10);
System.out.println("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.

Java implementation

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

Java 25
class Queue {
int capacity; // Maximum size of the queue
int[] data; // Array to store elements
int front; // Index of the front element
int rear; // Index of the rear element
int size; // Number of elements currently in the queue
public Queue(int capacity) {
this.capacity = capacity;
this.data = new int[capacity];
this.front = 0;
this.rear = -1;
this.size = 0;
}
public Queue() {
this(10);
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
// Check if the queue is full
return size == capacity;
}
}
public class Main {
public static void main(String[] args) {
Queue 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)
System.out.println("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.

Example: ...