Data Structure and Algorithms — Queue

Ahsan Majeed
3 min readAug 1, 2023

--

What is a Queue data structure?

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, where the first element added to the queue is the first one to be removed. Think of it as a line of people waiting, where the first person to join the line is the first one to be served and leave.

Representation of a Queue:

A queue can be implemented using an array or a linked list. In both cases, we need to keep track of the front and rear of the queue.

Basic Operations on Queue :

1. Enqueue (Insertion): Add an element to the rear (end) of the queue : O(1)

2. Dequeue (Deletion): Remove the element from the front (beginning) of the queue : O(1).

3. Front (Peek): Retrieve the element at the front of the queue without removing it : O(1).

4. isEmpty: Check if the queue is empty : O(1)

5. Size: Get the number of elements currently in the queue : O(1)

Below is a simple Java implementation of a queue using a LinkedList:

import java.util.LinkedList;

public class Queue<T> {
private LinkedList<T> list = new LinkedList<T>();

// Add an element to the rear of the queue
public void enqueue(T item) {
list.addLast(item);
}

// Remove the element from the front of the queue
public T dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty.");
}
return list.removeFirst();
}

// Get the element at the front of the queue without removing it
public T front() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty.");
}
return list.getFirst();
}

// Check if the queue is empty
public boolean isEmpty() {
return list.isEmpty();
}

// Get the number of elements in the queue
public int size() {
return list.size();
}
}

Real-Life Use Cases in Software Engineering where Queue Data Structure is Helpful:

1. Task Queues: In multi-threaded applications, a queue is often used to manage tasks or jobs that need to be processed. Workers/threads can dequeue tasks from the queue and process them in a controlled and efficient manner.

2. Message Queues: In distributed systems and microservices architectures, message queues play a crucial role in decoupling components. Messages are put in a queue and processed asynchronously by different services.

3. Breadth-First Search (BFS) in Graphs: Queues are used in graph traversal algorithms like BFS, where nodes are visited in the order they are enqueued.

4. Print Spooling: When multiple print jobs are sent to a printer, they are typically placed in a queue, and the printer processes them one by one in the order they were received.

5. Web Server Request Handling: Web servers can use a queue to manage incoming requests. Each request is added to the queue, and the server processes them sequentially.

In software engineering, queues quietly orchestrate efficiency, bringing order to chaos with their First-In-First-Out magic.

--

--

Ahsan Majeed

My thoughts and world. Just want to keep sharing thoughts, experiments & new stuff.