Search⌘ K
AI Features

Key Operations of a Queue

Understand and implement essential queue operations such as enqueue, dequeue, peek, IsEmpty, IsFull, and GetSize in C#. This lesson helps you master how queues maintain FIFO order and achieve efficient constant time operations, essential for coding reliable data structures.

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.

C# 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.

C# 14.0
using System;
public 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 = 10)
{
this.capacity = capacity;
data = new int?[capacity];
front = 0;
rear = -1;
size = 0;
}
public bool IsEmpty()
{
// Check if the queue is empty
return size == 0;
}
}
public class Solution
{
public static void Main()
{
// Create an empty queue
Queue queue = new Queue(10);
Console.WriteLine("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.

C# implementation

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

C# 14.0
using System;
public class Queue
{
public int Capacity; // Maximum size of the queue
public int?[] Data; // Array to store elements
public int Front; // Index of the front element
public int Rear; // Index of the rear element
public int Size; // Number of elements currently in the queue
public Queue(int capacity = 10)
{
Capacity = capacity;
Data = new int?[capacity];
Front = 0;
Rear = -1;
Size = 0;
}
public bool IsEmpty()
{
// Check if the queue is empty
return Size == 0;
}
public bool IsFull()
{
// Check if the queue is full
return Size == Capacity;
}
}
public class Solution
{
public static void Main()
{
// Create a queue with capacity 5
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)
Console.WriteLine("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. ...