00:00

Queue Data Structure

Last updated:

A queue stores elements in the order in which they arrive and hands them out in that same order. It is the data structure behind job scheduling, printer spooling, message processing, buffering and breadth-first search.

This tutorial covers what a queue is, its operations and their cost, how it is implemented, and the main kinds of queue.

What is a Queue Data Structure?

A queue is a linear data structure that follows FIFO order (First In, First Out): the element that has been waiting longest is the next one to leave. Elements cannot be inserted or removed in the middle.

A queue has two ends. New elements join at the rear (also called the tail), and elements leave from the front (also called the head).

Queue: elements are added at the rear and removed from the front

A line at a cinema ticket counter works the same way: whoever joined the line first is served first, and newcomers join at the back.

Real-life queue: people waiting in line are served in the order they arrived

Because the order of service is fair and predictable, queues are used wherever work arrives faster than it can be handled.

Operations in Queue

  1. Enqueue: add an element at the rear.

  2. Dequeue: remove the element at the front and return it.

  3. Peek or front: return the front element without removing it.

  4. rear: return the element at the rear without removing it (not every implementation offers this).

  5. isFull: tell whether a fixed-capacity queue has no free slot.

  6. isEmpty: tell whether the queue has no elements.

Complexity Analysis of Queue Operations

With a circular array or a linked list that keeps both a head and a tail reference, every operation works at one end only and takes constant time:

Operations Time Complexity Space Complexity
enqueue O(1)

O(1)

dequeueO(1)

O(1)

front

O(1)

O(1)

sizeO(1)

O(1)

isEmptyO(1)

O(1)

isFull

O(1)

O(1)

A plain array that shifts all elements left on every dequeue would make dequeue O(n); the circular queue described below avoids this.

Implementation of Queue Data Structure

A queue is usually built in one of two ways:

  1. Queue Implementation Using Array

  2. Queue Implementation Using Linked List

Types of Queue

There are four common kinds of queue:

Simple Queue or Linear Queue

The basic FIFO queue described above: insert at the rear, remove from the front. When it is stored in an array without wrap-around, the slots freed at the front cannot be reused, so the queue can report "full" while slots are actually free.

Circular Queue

A circular queue is a FIFO queue stored in an array whose last position is treated as being followed by the first. When the rear reaches the end of the array, it wraps around to index 0 and reuses the slots freed by earlier dequeues.

This keeps both enqueue and dequeue at O(1) without wasting space, which is why circular queues are used for buffers in operating systems, network devices and streaming code.

A circular queue is also called a ring buffer.

Circular Queue Representation

Circular queue: array positions arranged in a ring with front and rear indexes

Read more...

Priority Queue

In a priority queue, every element has a priority, and the element with the highest priority (or, in Java's PriorityQueue, the smallest element by default) is removed first, regardless of when it arrived.

Characteristics of a priority queue:

  • Each element has a priority, given by its value or by a comparator.
  • Removal always returns the element with the best priority.
  • Elements with equal priority are not guaranteed to come out in arrival order; java.util.PriorityQueue breaks such ties arbitrarily. If FIFO order among equals is needed, add a sequence number to the comparison.

Read more...

Double Ended Queue or Deque

Deque is short for "double-ended queue".

A deque allows insertion and removal at both the front and the rear. Used from one end only it behaves as a stack; used by adding at one end and removing at the other it behaves as a queue.

That flexibility makes it useful whenever elements must be taken from either end, for example in sliding-window algorithms or work-stealing schedulers.

The following diagram shows a deque.

Deque: elements can be inserted and removed at both the front and the rear

Read more...