Data StructuresBasicsbeginner

Queue Data Structure Explained for Beginners

A queue explained with a ticket counter line: FIFO, enqueue, dequeue and peek, a small circular queue in Java and Java's Queue interface.

Published
TL;DR – Quick Answer

A queue is a data structure where items join at one end, called the rear, and leave from the other end, called the front. The first item added is the first one removed, which is called FIFO (First In, First Out). Its main operations are enqueue (add at the rear), dequeue (remove from the front) and peek (look at the front item). Each takes O(1) time in a good implementation.

On This Page

A queue is a way of storing items in a line. New items join at the back, called the rear, and items leave from the front, called the front. The first item that joined is the first one to leave. This rule is called FIFO: First In, First Out.

This page uses a ticket counter line to explain the idea, then walks through enqueue, dequeue and peek, and ends with a small Java program you can run. If you have already read our stack data structure guide, a queue is its opposite.

Queue in simple words: the ticket-line analogy

Picture the line at a railway ticket counter or a cinema box office.

  • A new person always joins at the back of the line.
  • The person at the front is served and leaves.
  • Nobody jumps the line or leaves from the middle.

Whoever came first is served first. That is exactly how a queue works.

At the ticket counter In a queue
A person joins at the back enqueue
The person at the front is served and leaves dequeue
Seeing who is next without serving them peek
Nobody waiting The queue is empty
The hall cannot hold more people The queue is full (only for fixed-size queues)

FIFO explained with a picture

Here is a queue after adding 101, then 102, then 103:

 front                 rear
   |                     |
   v                     v
 +-----+-----+-----+
 | 101 | 102 | 103 |
 +-----+-----+-----+
 leaves here      joins here

101 joined first, so it leaves first. 103 joined last, so it waits until 101 and 102 have left.

The opposite rule is LIFO (Last In, First Out), used by a stack. See stack vs queue for a full side-by-side comparison.

Queue operations: enqueue, dequeue and peek

Operation What it does Time
enqueue(x) Adds x at the rear O(1)
dequeue() Removes the front item and returns it O(1)
peek() Returns the front item without removing it O(1)
isEmpty() Tells you if the queue has no items O(1)
size() Number of items O(1)

Enqueue and dequeue step by step

Start:            (empty)
enqueue(101):     [101]               front = 101
enqueue(102):     [101, 102]          front = 101
enqueue(103):     [101, 102, 103]     front = 101
dequeue():        [102, 103]          returns 101
peek():           [102, 103]          returns 102, nothing removed
enqueue(104):     [102, 103, 104]
dequeue():        [103, 104]          returns 102

Read the lists left to right as front to rear.

Why a simple array queue wastes space

Suppose you build a queue on an array of size 5 and keep two indexes: front and rear. Each dequeue moves front forward. After a few dequeues, the slots at the start of the array are empty, but rear has already reached the end:

index:   0     1     2     3     4
       [ -  |  -  | 103 | 104 | 105 ]
                    ^           ^
                  front        rear

There is room for two more items, but a simple array queue would say it is full. Shifting every item left after each dequeue fixes this, but that makes dequeue O(n).

The better fix is a circular queue. When rear reaches the last index, it wraps around to index 0. The formula is:

rear = (rear + 1) % capacity

The same formula moves front. Now every slot is reused and every operation stays O(1).

A small circular queue in Java

This program builds a circular queue on an array of size 5. It keeps a size counter so it can tell "empty" from "full".

public class QueueDemo {
    static int[] items = new int[5];
    static int front = 0;
    static int rear = -1;
    static int size = 0;

    static void enqueue(int value) {
        if (size == items.length) {
            System.out.println("Queue is full, cannot add " + value);
            return;
        }
        rear = (rear + 1) % items.length;
        items[rear] = value;
        size++;
    }

    static int dequeue() {
        if (size == 0) {
            System.out.println("Queue is empty");
            return -1;
        }
        int value = items[front];
        front = (front + 1) % items.length;
        size--;
        return value;
    }

    static int peek() {
        if (size == 0) {
            System.out.println("Queue is empty");
            return -1;
        }
        return items[front];
    }

    public static void main(String[] args) {
        enqueue(101);
        enqueue(102);
        enqueue(103);
        System.out.println("Front: " + peek());
        System.out.println("Served: " + dequeue());
        System.out.println("Served: " + dequeue());
        enqueue(104);
        enqueue(105);
        enqueue(106);
        enqueue(107);
        enqueue(108);
        System.out.println("Front now: " + peek() + ", people waiting: " + size);
    }
}

Output:

Front: 101
Served: 101
Served: 102
Queue is full, cannot add 108
Front now: 103, people waiting: 5

Save it as QueueDemo.java and run java QueueDemo.java with Java 11 or later.

What happened:

  • 101, 102 and 103 go into indexes 0, 1 and 2.
  • Two dequeues serve 101 and 102. front moves to index 2.
  • 104 and 105 go into indexes 3 and 4. Then rear wraps around, so 106 goes into index 0 and 107 into index 1.
  • The queue now holds 5 items, so 108 is refused.
  • The front is still 103, because it has waited the longest.

Common mistake: Checking "full" with rear == items.length - 1. In a circular queue, rear can be at the last index while there is free space at the start. Keep a size counter, as above, and compare it with the capacity.

A queue can also be built on a linked list with a head and a tail reference: enqueue adds after the tail, dequeue removes the head. There is then no fixed capacity.

Using a queue from the Java library

In real Java code, you use the Queue interface with ArrayDeque (or LinkedList):

import java.util.ArrayDeque;
import java.util.Queue;

public class BuiltInQueue {
    public static void main(String[] args) {
        Queue<String> line = new ArrayDeque<>();
        line.offer("Ravi");
        line.offer("Priya");
        line.offer("Kiran");
        System.out.println(line);
        System.out.println("Next: " + line.peek());
        System.out.println("Served: " + line.poll());
        System.out.println(line + " size " + line.size());
        line.poll();
        line.poll();
        System.out.println("Empty poll gives: " + line.poll());
    }
}

Output:

[Ravi, Priya, Kiran]
Next: Ravi
Served: Ravi
[Priya, Kiran] size 2
Empty poll gives: null
Action Throws an exception if it fails Returns a special value
Add at rear add(x) offer(x) returns false
Remove front remove() poll() returns null
Look at front element() peek() returns null

Beginners usually use offer, poll and peek, and check for null.

Types of queues

Type How it works Example use
Simple queue Add at rear, remove at front Ticket line
Circular queue Array queue where indexes wrap around Fixed-size buffers, such as keyboard input
Priority queue The item with the highest priority leaves first, not the oldest Hospital emergency room, task schedulers; Java's PriorityQueue
Deque (double-ended queue) Add and remove at both ends Java's ArrayDeque, which can act as a stack or a queue

Where queues are used

  • Printers: documents print in the order they were sent.
  • Operating systems: processes waiting for the CPU sit in queues. Round-robin scheduling gives each one a turn in order.
  • Web servers: incoming requests wait in a queue when the server is busy.
  • Messaging systems such as Kafka and RabbitMQ pass messages between services in order.
  • Breadth-first search (BFS) in graphs and trees visits nodes level by level using a queue.
  • Call centres and food delivery apps hold waiting customers or orders in a queue.

Queue vs stack in one table

Point Queue Stack
Rule FIFO, first in first out LIFO, last in first out
Add enqueue, at the rear push, at the top
Remove dequeue, from the front pop, from the top
Everyday example Ticket counter line Pile of plates
Graph search that uses it BFS DFS

Read stack vs queue for more differences and interview questions on both.

Practice problems for beginners

  1. Reverse the first k items of a queue.
  2. Build a queue using two stacks.
  3. Generate binary numbers from 1 to n using a queue (1, 10, 11, 100, ...).
  4. Find the first non-repeating character in a stream of letters.
  5. Print a binary tree level by level.

Quick revision

  • A queue adds at the rear and removes from the front.
  • It follows FIFO: first in, first out.
  • enqueue adds, dequeue removes, peek looks. All are O(1) in a circular or linked queue.
  • A circular queue reuses array slots with (index + 1) % capacity.
  • In Java, use Queue<T> q = new ArrayDeque<>() with offer, poll and peek.

Next, compare it again with the stack data structure, then continue with our data structures and algorithms guide and the DSA interview questions.

Frequently Asked Questions

What is a queue in simple words?
A queue is a line of items where new items join at the back and items leave from the front, like people waiting at a ticket counter. Whoever came first is served first.
What does FIFO mean?
FIFO stands for First In, First Out. The item that was added earliest is the first one removed.
What are enqueue and dequeue?
Enqueue adds an item at the rear of the queue. Dequeue removes the item at the front and returns it.
What is a circular queue?
A circular queue is an array-based queue where the rear wraps around to the start of the array when it reaches the end. This reuses the empty slots left behind by dequeued items.
Which class should I use for a queue in Java?
Use the Queue interface with ArrayDeque or LinkedList. offer adds, poll removes and peek reads the front. poll and peek return null when the queue is empty.
What is the difference between a queue and a stack?
A queue removes the oldest item first (FIFO). A stack removes the newest item first (LIFO).

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us