Search⌘ K
AI Features

Key Operations of a Queue

Explore the key operations of a queue in Go, including checking if it is empty or full, enqueueing to add elements, dequeueing to remove them, peeking at the front element, and retrieving the size. Understand how these operations maintain efficiency with constant time complexity, supporting effective FIFO data management.

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.

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

Go (1.24.2)
package main
import "fmt"
type Queue struct {
capacity int // Maximum size of the queue
data []int // Slice to store elements
front int // Index of the front element
rear int // Index of the rear element
size int // Number of elements currently in the queue
}
func NewQueue(capacity int) *Queue {
return &Queue{
capacity: capacity,
data: make([]int, capacity),
front: 0,
rear: -1,
size: 0,
}
}
func (q *Queue) isEmpty() bool {
// Check if the queue is empty
return q.size == 0
}
func main() {
// Create an empty queue
queue := NewQueue(10)
fmt.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() method, 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.

Go implementation

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

Go (1.24.2)
package main
import "fmt"
type Queue struct {
capacity int // Maximum size of the queue
data []int // Slice to store elements
front int // Index of the front element
rear int // Index of the rear element
size int // Number of elements currently in the queue
}
func NewQueue(capacity int) *Queue {
return &Queue{
capacity: capacity,
data: make([]int, capacity),
front: 0,
rear: -1,
size: 0,
}
}
func (q *Queue) isEmpty() bool {
// Check if the queue is empty
return q.size == 0
}
func (q *Queue) isFull() bool {
// Check if the queue is full
return q.size == q.capacity
}
func main() {
// Create a queue with capacity 5
queue := NewQueue(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)
fmt.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 ...