5.1Queue with an Array
The problem with a linear queue: wasted cells
The queue you built with an array sometimes says "full" while it holds only two elements. This is not a bug in the code; it comes from the way a linear queue works. This lesson shows where the problem comes from, three ways to fix it and what each one costs.
- Why cells go to waste in a linear queue
- Why shifting the elements makes dequeue O(n)
- How a circular queue fixes the problem in O(1)
1The queue says full, but there is room
Start with an empty queue of capacity 5. Run these operations in order and watch the two indexes. The last column is the number of cells left of front that can no longer be used.
| Operation | front | rear | Elements | Wasted cells |
|---|---|---|---|---|
| enqueue(3) | 0 | 0 | 1 | 0 |
| enqueue(8) | 0 | 1 | 2 | 0 |
| enqueue(5) | 0 | 2 | 3 | 0 |
| dequeue() → 3 | 1 | 2 | 2 | 1 |
| dequeue() → 8 | 2 | 2 | 1 | 2 |
| enqueue(6) | 2 | 3 | 2 | 2 |
| enqueue(9) | 2 | 4 | 3 | 2 |
| dequeue() → 5 | 3 | 4 | 2 | 3 |
| enqueue(7) | 3 | 4 | 2 | 3: queue is full |
In the last row the queue holds only 6 and 9. Three of the five cells are free. Even so, isFull returns true and 7 is not added.
2Why does this happen?
Dequeue moves front to the right when it removes an element. Enqueue moves rear to the right when it adds one. Both indexes only move right, and neither ever comes back. The cells left of front fall outside the queue and are never written again.
The full check rear == CAPACITY - 1 only looks at the right end. So the queue counts as full once it reaches the end of the array; the free space at the start is not counted. In a long-running program every element uses up one cell: with capacity 5, the queue can take only 5 enqueues in its whole life.
3Fix 1: Start over when it empties
When the queue becomes completely empty, you can move both indexes back to the start. These lines are added at the end of dequeue:
q->front++;if (q->front > q->rear) { // the queue is now empty q->front = 0; q->rear = -1;}This is cheap and stays O(1). But it only helps when the queue empties completely. In the example above the queue never empties, so the problem stays exactly the same. It is a partial fix.
4Fix 2: Shift the elements left
If you shift the remaining elements one cell to the left after every dequeue, front always stays at 0 and no cell is wasted:
bool dequeue(struct Queue* q, int* value) { if (isEmpty(q)) { return false; } *value = q->items[0]; // front is always 0 for (int i = 1; i <= q->rear; i++) { q->items[i - 1] = q->items[i]; // shift left } q->rear--; return true;}The cost is heavy. Every dequeue moves all the elements left in the queue. With n elements there are n - 1 copies, and dequeue becomes O(n). This is the same cost as deleting from the front of an array. In a queue of one million elements, every dequeue means nearly a million copies.
5Fix 3: The circular queue
The real fix is to think of the array as a ring. When rear reaches the last cell, the next write goes to cell 0, as long as that cell is free. The same goes for front. The mod operation does the wrapping: rear = (rear + 1) % CAPACITY. No element is shifted, and every operation stays O(1).
On a ring front and rear can swap sides, so rear < front no longer means empty. That is why a circular queue keeps the element count separately. The details start in What is a circular queue?.
6Comparison
| Approach | Dequeue | Freed cell | Note |
|---|---|---|---|
| Linear queue | O(1) | Never used again | Says full while there is room |
| Reset when empty | O(1) | Comes back only when the queue empties completely | Partial fix |
| Shift left | O(n) | Used again at once | Slow for a large queue |
| Circular queue | O(1) | Used again at once | Keeps a separate element count |
A queue built with a linked list does not have this problem at all: a removed node is freed, and new memory is taken for a new node. See Creating a linked-list queue.
7Try it
On a linear queue of capacity 4 you run 4 enqueues and then 4 dequeues (no reset). What does enqueue(1) do now?
The answer is B. front is 4 and rear is 3. The queue is empty, but rear == CAPACITY - 1 holds, so isFull returns true. Empty and full at the same time: that is the linear queue's problem.
Show the answer
The answer is B. front is 4 and rear is 3. The queue is empty, but rear == CAPACITY - 1 holds, so isFull returns true. Empty and full at the same time: that is the linear queue's problem.
What does it cost to win back free cells by shifting the elements?
The answer is A. On every dequeue, all the remaining elements move one cell to the left. n elements need n - 1 copies. Enqueue is still O(1).
Show the answer
The answer is A. On every dequeue, all the remaining elements move one cell to the left. n elements need n - 1 copies. Enqueue is still O(1).
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.