DEV Community

Cover image for Understanding Queue in Data Structures with Real-Life Examples
Gihan Benaragama
Gihan Benaragama

Posted on

Understanding Queue in Data Structures with Real-Life Examples

Have you ever stood in a line at a supermarket? The person who arrived first gets served first, and newcomers join at the back. That simple idea is exactly what a Queue is in computer science. Let's break it down step by step.

What Is a Queue?

A queue is a linear data structure that follows a simple rule:

First In, First Out (FIFO)

This means the first element added to the queue will be the first one to be removed.

Think of it like a real-world queue at a ticket counter. The first person who joins the line will be served first.

ticket counter

How Does a Queue Work?

A queue has two ends:

  • Front — where items are removed (think: the front of the checkout line)
  • Rear — where items are added (think: the back of the line)
  • You can only add to the rear and remove from the front. You cannot touch items in the middle.

Three Basic Operations

  1. Insert (Enqueue)

Adding a new item to the rear of the queue.
Example: The queue currently holds [15, 27, 39, 23]. When we insert 45, it goes to the rear:

Before: Front → [15, 27, 39, 23] ← Rear
After: Front → [15, 27, 39, 23, 45] ← Rear

insert

  1. Remove (Dequeue)

Taking an item out from the front of the queue.
Example: From [15, 27, 39, 23], removing takes 15 (the front item):

Before: Front → [15, 27, 39, 23] ← Rear
After: Front → [27, 39, 23] ← Rear

remove

  1. PeekFront

Reading the value at the front without removing it. You're just looking, not taking.

Example: PeekFront on [15, 27, 39, 23] returns 15, and the queue stays the same.

peek

Where Are Queues Used in Real Life (in Computers)?

  • Printer queue — When you send multiple documents to a printer, they line up and print one by one in the order they were sent.
  • Keyboard input — When you type quickly, each keystroke is stored in a queue so none are lost.
  • Pipelines — Data flowing through processes in an operating system uses queues to stay ordered.

When Do You Actually Need a Queue in a Project?

Tasks pile up faster than they're processed
A user uploads a video. Encoding takes 2 minutes. You can't make them wait — so you drop the task in a queue and handle it in the background.

You don't want to block the user
Sending a welcome email, generating a PDF, resizing a photo — these are slow. Push them to a queue. The user gets an instant response, the work happens quietly behind the scenes.

Sending notifications at scale
Emails, SMS, push notifications — all go through a queue. You collect them, then deliver them without slowing down your main app.

Implementing a Queue in Java

Let's look at how we actually build a queue in code. We'll use a Java class called QueueX.

  • Setting Up the Queue
class QueueX {
    private int maxSize;    // maximum number of items the queue can hold
    private int[] queArray; // the actual array storing items
    private int front;      // index of the front item
    private int rear;       // index of the rear item
    private int nItems;     // how many items are currently in the queue

    public QueueX(int s) {  // constructor: called when creating a new queue
        maxSize = s;
        queArray = new int[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

Think of this as setting up an empty line — you reserve space for a certain number of people (maxSize), and right now nobody's in the queue (nItems = 0).

setting up

  • Insert Method
public void insert(int j) {
    if (rear == maxSize - 1)
        System.out.println("Queue is full");
    else {
        queArray[++rear] = j;  // move rear forward, then place the new item
        nItems++;
    }
}
Enter fullscreen mode Exit fullscreen mode

Before inserting, we check if the queue is already full - if (rear == maxSize - 1)

If not, we move the rear pointer forward by one [++rear] and place the new item there.

insert

  • Remove Method
public int remove() {
    if (nItems == 0) {
        System.out.println("Queue is empty");
        return -99;  // signal that queue was empty
    } else {
        nItems--;
        return queArray[front++];  // return the front item, then move front forward
    }
}
Enter fullscreen mode Exit fullscreen mode

We check if the queue is empty before removing - if (nItems == 0)
If there are items, we return the one at front and move the front pointer forward.

delete

  • PeekFront Method
public int peekFront() {
    if (nItems == 0) {
        System.out.println("Queue is empty");
        return -99;
    } else {
        return queArray[front];  // look at front item WITHOUT moving the pointer
    }
}
Enter fullscreen mode Exit fullscreen mode

Notice the difference from remove() — here we do NOT do front++. We just look.

Peek - We just print current front value

A Problem: The Queue Gets Stuck!

Think You have a queue with 7 slots (index 0 to 6):

[67, 12, 22, 55, 34, 70, 60] ← full

Front=0, Rear=6

You remove 3 items. Now:

[_, _, _, 55, 34, 70, 60]
Front=3, Rear=6

There are 3 empty slots at the beginning, but rear is already at the last index (6). If you try to insert a new item, the code thinks the queue is full — even though it's not!

problem

The Solution: Circular Queue

A circular queue (also called a ring buffer) fixes this problem by wrapping the rear pointer back to the beginning when it reaches the end.

Think of it as a circular track — when you reach the end, you loop back to the start.

solution

How Insert Changes for a Circular Queue

public void insert(int j) {
    if (nItems == maxSize)
        System.out.println("Queue is full");
    else {
        if (rear == maxSize - 1)
            rear = -1;           // wrap around to the beginning!
        queArray[++rear] = j;
        nItems++;
    }
}
Enter fullscreen mode Exit fullscreen mode

How Remove Changes for a Circular Queue

public int remove() {
    if (nItems == 0) {
        System.out.println("Queue is empty");
        return -99;
    } else {
        int temp = queArray[front++];
        if (front == maxSize)
            front = 0;           // wrap front around too!
        nItems--;
        return temp;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now both front and rear wrap around, so we never waste those empty slots at the beginning!

Putting It All Together: A Full Example

class QueueApp {
    public static void main(String[] args) {
        QueueX theQueue = new QueueX(10); // create a queue that holds 10 items

        theQueue.insert(10);
        theQueue.insert(25);
        theQueue.insert(55);
        theQueue.insert(65);
        theQueue.insert(85);

        while (!theQueue.isEmpty()) {     // keep removing until empty
            int val = theQueue.remove();
            System.out.print(val + " ");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:10 25 55 65 85

The items come out in exactly the order they went in — that's FIFO in action!

Key Takeaways

  • A queue is like a real-world line — first come, first served.
  • You add at the rear and remove at the front.
  • You can only peek at the front item, not anything in the middle.
  • A basic linear queue can run into a "false full" problem.
  • A circular queue solves this by wrapping around the array.

That's it! Queues are one of the most practical data structures in computing, and now you know exactly how they work — from the idea all the way down to the code.

What if the person at the back of the line actually deserves to go first? That's exactly what a Priority Queue solves. See you in the next one!

Top comments (0)