Essential Data Structures home

Queue

A line where the first item in is the first item out. New items join at the back, and items leave from the front. First in, first out.

A B C front rear dequeue enqueue Enqueue adds at the rear. Dequeue removes from the front. A leaves first.
Illustration: a queue of three items. Items enter at the rear and leave from the front, in the order they arrived.

What it is

A queue is a collection with one rule: items leave in the order they arrived. Enqueue adds a new item at the rear. Dequeue takes the front item off. Peek looks at the front item without removing it. The item at the front is always the one that has waited longest.

That single rule is the point. By giving up random access, a queue gets a very simple promise: nobody jumps the line. The order of removal is the exact order of arrival.

A queue is the mirror image of a stack. A stack serves the most recent item first, a queue serves the oldest item first. Both are built on the same parts, an array or a linked list, with the ends used differently.

How it works

Enqueue, dequeue, and peek each touch only an end, so each takes constant time. There is nothing to search and nothing to shift, as long as the queue is built the right way. The only failures are the two edges: adding to a full fixed-size queue is an overflow, and removing from an empty queue is an underflow. A dynamic queue grows instead of overflowing, and a careful program checks for empty before it dequeues.

The classic implementation is a circular buffer: a fixed array where the rear wraps around to the start when it reaches the end, so the space freed by dequeues is reused. A linked list works too, with the head as the front and a pointer to the tail for fast enqueue. What does not work well is a plain list used with pop(0), because removing the first item shifts every item after it, which costs linear time. Python's collections.deque exists to avoid exactly that.

Complexity

OperationAverageWorst case
Enqueue (add at rear)O(1)O(1) amortized on a dynamic array
Dequeue (remove from front)O(1)O(1)
Peek (front)O(1)O(1)
Search by valueO(n)O(n)

A circular buffer or a linked list gives O(1) enqueue and dequeue in the worst case. Removing from the front of a plain list costs O(n), because every remaining item has to shift.

When to use it, and when not to

Use a queue when items should be handled in the order they arrive: tasks waiting for a worker, requests waiting for a server, print jobs, messages waiting to be delivered, and breadth-first traversal of a graph or a tree. If fairness matters and nobody should jump ahead, a queue is the shape.

Do not use a queue when you need to reach an item in the middle, find something by value, or process the most recent item first. That last case is a stack, the queue's mirror image, and reaching into the middle is what an array is for.

Where you meet it

Everywhere a program waits its turn. A printer queue holds jobs in order. A web server holds incoming requests in a queue. A messaging app holds undelivered messages in a queue. An operating system schedules processes with queues, and a breadth-first search explores a maze level by level with one too.

In Python

from collections import deque

queue = deque()

queue.append("A")   # enqueue
queue.append("B")   # enqueue
queue.append("C")   # enqueue

print(queue[0])        # peek: A
print(queue.popleft()) # dequeue: A, then B is at the front
print(queue.popleft()) # dequeue: B

In short: a queue is fairness, not storage. Two ends, one rule, and the item that waited longest always comes out first.

Sources

← Home