5Queue
What is a queue?
A queue is a structure where elements go in at one end and come out at the other. There is a single rule: the first one in is the first one out. A queue works just like a line at the bank. This lesson covers what a queue is, which operations it offers, how it is built and which kinds of queue there are.
- The FIFO order and how it differs from the LIFO order of a stack
- The queue operations and the roles of front and rear
- How a queue is built with an array or a linked list, its kinds and where it is used
1First in, first out
Think of standing in line at a bank. A newcomer joins the end of the line. When a desk is free, the person at the front is called. Nobody cuts in and nobody skips a turn. A queue works exactly like that. This order is called FIFO: First In, First Out.
The same order is everywhere on a computer:
- The print queue: the documents you send to print line up. The printer prints the one sent first.
- The keyboard buffer: keys you press while the computer is busy wait in a queue and are handled in the order you pressed them.
- A call center: callers are put in a waiting line; the first caller is connected first.
A stack is the opposite: there the last one in is the first one out (LIFO). A stack has one end; a queue has two ends, and each one has its own job.
2Two ends: front and rear
The end where elements leave is called the front, and the end where they enter is called the rear. A new element is always added behind rear; the element that leaves is always the one at front. You cannot reach an element in the middle directly.
So queue code keeps track of two places. In an array these are two indexes; in a linked list they are two node addresses. A stack needed only one top.
3A queue is an abstract data type
A queue is defined by the operations it offers, not by how it is built. A definition like this is called an abstract data type (ADT). Code that uses a queue only knows the operations below; it does not know whether there is an array or a linked list inside.
| Operation | What it does | What it returns |
|---|---|---|
| enqueue(x) | Adds x at the rear | Nothing (in C, success: true or false) |
| dequeue() | Removes the front element | The removed element |
| peek() | Looks at the front element without removing it | The front element |
| isEmpty() | Is the queue empty? | true or false |
| isFull() | Is the queue full? (only for a fixed-size queue) | true or false |
In a good implementation all of these take O(1) time: however many elements the queue holds, only the two ends are touched. enqueue and dequeue are the standard names; some books also say "add to the queue" and "remove from the queue".
4Let us trace a sequence of operations
Start with an empty queue and run these operations in order. In the queue column the leftmost element is at the front and the rightmost is at the rear.
| Operation | Queue (front → rear) | Returned value |
|---|---|---|
| enqueue(3) | 3 | |
| enqueue(8) | 3 8 | |
| enqueue(5) | 3 8 5 | |
| dequeue() | 8 5 | 3 |
| peek() | 8 5 | 8 |
| enqueue(1) | 8 5 1 | |
| dequeue() | 5 1 | 8 |
| dequeue() | 1 | 5 |
| isEmpty() | 1 | false |
Look at the values the dequeues returned: 3, 8, 5. The elements left in the order they came in. On a stack, the same operations would have made the first pop return 5.
5Empty and full queues
Two operations cannot always run. You cannot dequeue or peek on an empty queue, because there is no element to remove or look at. In a fixed-size array you cannot enqueue once there is no room left. So isFull is checked before enqueue, and isEmpty before dequeue and peek.
How this is reported depends on the language. In the C code on this site the function returns false; Java throws IllegalStateException and Python raises IndexError.
6Two ways to build a queue
With an array: the elements sit in an array. front is the index of the front element and rear is the index of the rear element. An empty queue has front at 0 and rear at -1. Details are in Creating an array queue.
The array has a problem: dequeue moves front to the right, and the cells at the start are never used again. The wasted cells lesson shows this. The fix is to wrap around to the start at the end of the array: the circular queue.
With a linked list: each element is a node. front points at the first node and rear at the last one. Enqueue adds a node at the end, dequeue removes the first node. There is no size limit, but each node carries one extra pointer. Details are in Creating a linked-list queue.
If you link the last node back to the first, one pointer is enough: the queue with a circular linked list. All implementations offer the same operations under the same names; when to choose which is covered in Array or linked list?.
7Kinds of queue
- Linear queue: the simplest array queue. It cannot use the cells freed at the start.
- Circular queue: behaves as if the end of the array were joined to its start; freed cells are used again. See What is a circular queue?.
- Priority queue: an element leaves by its priority, not by when it arrived. Think of the line at an emergency room. See What is a priority queue?.
- Deque: a queue where you can add and remove at both ends. See Deque: the double-ended queue.
The circular queue and the deque keep or extend the FIFO order. The priority queue is not FIFO; the word "queue" in its name only comes from its operations.
8Where is a queue used?
- Operating systems: jobs waiting to run sit in a queue. Each job gets a short slice of time in turn. See Round-robin.
- Printers and file jobs: incoming jobs are done in the order they arrived.
- Network buffers: data packets arriving at a computer wait in a queue until they are handled.
- Breadth-first search (BFS): in a graph or a maze, nearby points are visited first; the points waiting their turn are kept in a queue.
- A queue from two stacks: a queue can also be built from two stacks. See A queue from two stacks.
9What this module covers
- Queue with an array: creating, enqueue, dequeue, peek and wasted cells.
- Circular queue: what it is, enqueue, dequeue and full or empty.
- Queue with a linked list: the four operations and a comparison of the two implementations; then the queue with a circular linked list.
- Priority queue: what it is, inserting and removing with an array or a linked list, and which structure to choose.
- More: deque, a queue from two stacks, round-robin and exam questions.
10Try it
On an empty queue you run enqueue(4), enqueue(7), dequeue(), enqueue(2). What does peek() return now?
The answer is B. Dequeue removes 4, which came in first. 7 is left, and then 2 joins behind it. The front element is 7. On a stack the answer would be 2.
Show the answer
The answer is B. Dequeue removes 4, which came in first. 7 is left, and then 2 joins behind it. The front element is 7. On a stack the answer would be 2.
Which of these does not follow the FIFO order?
The answer is B. Undo reverses the latest change first: that is LIFO and is done with a stack. In the other two, whoever comes first is handled first.
Show the answer
The answer is B. Undo reverses the latest change first: that is LIFO and is done with a stack. In the other two, whoever comes first is handled first.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.