A queue is a linear data structure that adds items at the rear and removes them from the front, so the first item in is the first out (FIFO). On a circular array or a linked list, enqueue, dequeue and peek all take O(1) time. This page explains how it works, with step-by-step animations of a printer queue, a web server's request queue and a linked-list-based queue from the VisiGrab app.
// how queues work
A queue is a linear data structure that adds items at one end, the rear, and removes them from the other, the front, so the first item in is the first out (FIFO). It is an abstract data type (ADT): an array or a linked list can sit underneath, and the app builds it both ways. Front and rear are also called head and tail.
The first item added is the first one removed, as in a line at a counter. Enqueue 1, 2 and 3, and 1 comes out first. A stack, which is LIFO, would return 3 first.
Enqueue adds an item at the rear. Dequeue removes the front item and returns it. Peek, also called front, returns the front item and leaves it in place. Rear, or back, reads the last item. isEmpty and size tell whether the queue holds items and how many, and a bounded queue also has isFull. On a circular array or a linked list, each takes O(1) time, amortized when a dynamic array has to grow or shrink. There is no access by index. On an empty queue, dequeue and peek fail: this is underflow.
A shared printer may be busy when a document arrives. A print queue, kept by the print spooler, holds such documents, so nobody has to send one again. In the player, two computers send four documents, and the printer prints them in the order they arrived.
A web server keeps incoming requests in a request queue, in the order received. A pool of worker threads or processes takes them from the front. In the player, three clients send requests, the middle one first; the requests queue in order of arrival, not top to bottom, and the workers take them from the front in that order.
When every worker is busy, new requests wait in the queue. Thread pools, task queues and message queues such as RabbitMQ follow the same producer–consumer pattern.
In a linear queue on a plain array, dequeue would shift every item one cell left, as in deleting from the beginning of an array: O(n). Moving a front index forward instead is O(1), but the freed cells are never reused. Once rear reaches the last cell, the array looks full while cells at the start are free: a false overflow. A circular queue fixes both. It keeps the items in a circular buffer, or ring buffer, and wraps each index with the modulo operator: rear = (rear + 1) % capacity on enqueue, front = (front + 1) % capacity on dequeue. Both stay O(1), and every cell is reused.
Wrapped indices alone cannot tell a full queue from an empty one. The app's code starts with rear = -1, so its rear marks the last item, and it keeps a size count: the queue is full when size == capacity. Another convention keeps rear at the next free cell and leaves one cell empty: the queue is empty when front == rear and full when (rear + 1) % capacity == front. The app's dynamic version doubles a full array, copying the items in queue order. It halves the array at a quarter full; halving at half full would make alternating enqueues and dequeues resize every time. In the app you can enqueue and dequeue on a circular array and watch the markers wrap.
A linked-list-based queue keeps each item in a node with a link to the next. It uses a double-ended linked list: front links to the first node, where dequeue removes, and rear to the last, where enqueue adds. The other way round is slow: removing the last node needs the node before it, an O(n) walk. In the player, 4 is dequeued from 4, 5, 0, 1, and 7 is enqueued, leaving 5, 0, 1, 7.
Each operation changes only a couple of links, so both are O(1), with no capacity limit. An enqueue into an empty queue sets both front and rear, and dequeuing the last node sets both to null.
Note
In C and C++ a link is a pointer; in Java and Python it is a reference. In C++ a dequeued node must also be freed with delete.
Queue overflow is an enqueue onto a full queue. Only a bounded queue, such as a static circular array, can overflow; a queue on a dynamic array or a linked list fails only when memory runs out. Queue underflow is a dequeue or peek on an empty queue.
Enqueue adds an item at the rear of the queue; dequeue removes the item at the front, the one that has waited longest, and returns it. Try both on the interactive queue under Queue operations.
It is a queue on a circular array: its indices wrap from the last cell back to the first, so freed cells are reused, and enqueue and dequeue both stay O(1).
| Operation | Static circular array | Dynamic circular array | Linked list |
|---|---|---|---|
| Enqueue | O(1); fails when full | O(1) amortized, O(n) worst case | O(1) |
| Dequeue | O(1) | O(1) amortized, O(n) worst case | O(1) |
| Peek, isEmpty, size | O(1) | O(1) | O(1) |
| Search | O(n) | O(n) | O(n) |
Search is not a queue operation. Space is O(n); a static array reserves its whole capacity up front. A dynamic circular array allocates nothing per enqueue but copies everything when it resizes. A linked list never copies, but every enqueue allocates a node with an extra link.
A queue and a stack differ only in which item leaves: a stack removes the newest, a queue the oldest. So breadth-first search uses a queue, and depth-first search a stack. A queue can also be built from two stacks. Enqueue pushes onto the first; dequeue pops from the second, refilling it from the first only when it is empty. Each item moves across once, so both are O(1) amortized.
In Python, list.pop(0) shifts every remaining item, so collections.deque is used instead: append(), popleft() and d[0] all take O(1). popleft() on an empty deque raises IndexError. queue.Queue is thread-safe, and its get() blocks while it is empty. In Java, Queue is an interface. ArrayDeque, a growable circular array, is usually faster than LinkedList as a queue, and it rejects null. add, remove and element throw on failure, while offer, poll and peek return false or null. C++'s std::queue is an adaptor over std::deque by default, with push(), pop(), front() and back(). pop() returns nothing, so read front() first; on an empty queue, both are undefined behavior.
The full source code is on GitHub, in Python, Java and C++.
Queue interview problems include a queue from two stacks, design circular queue and sliding-window maximum with a deque.
// in the app
In VisiGrab the queue is an 18-slide show, from an enqueue/dequeue demo to the linked-list-based queue, played one step at a time.
A six-slot queue holding 7, 4, 2 and 0 next to the FIFO definition. Enq adds a random number at the rear and Deq removes the front, and each button switches off when the queue is full or empty.
An array of six cells holding 8, 3, 5 and 0, with front and rear markers that wrap round as you press Enq and Deq. A help dialog explains the circular array and the modulo rule, with the index update in your chosen language.
Step through every slide and step back, or jump to any slide from the list. The show reopens where you left it.
Two tabs: Info, with the key characteristics of a queue as an abstract data type, and Efficiency, with a complexity table for a linked list, a static array and a dynamic array.
Four queues in each language: on a static circular array (a bounded array in Python), on a dynamic array that doubles and halves, on an ArrayList, list or vector whose dequeue is O(n), and on a linked list. Notes on thrashing and memory sit between the listings, with links to the full source on GitHub.
Ten questions to check what you have learned before moving on.
// build your own
Download VisiGrab and step through the printer queue, the web server and the linked-list-based queue, then try the circular array yourself.
The queue is one of 30+ topics. See them all