algoritmauzmanı
Sign in
TREN
5.1Queue with an Array
Lesson 5.1.5

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.

What you will learn
  • 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)
5 min

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.

OperationfrontrearElementsWasted cells
enqueue(3)0010
enqueue(8)0120
enqueue(5)0230
dequeue() → 31221
dequeue() → 82212
enqueue(6)2322
enqueue(9)2432
dequeue() → 53423
enqueue(7)3423: 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.

3081526394frontrear
front = 3, rear = 4. 3, 8 and 5 in the faded cells left long ago; these cells will not be used again. Since rear is at the last cell, the queue counts as full.

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;}
6091234frontrear
The shifting queue after the same operations: 6 and 9 moved to the start of the array, front = 0, rear = 1. Three cells can be used again.

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 . 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 .

70126394frontrear
enqueue(7) on a circular queue: rear wrapped from 4 to 0 and 7 was written into the freed cell 0. The order is 6, 9, 7.

On a ring front and rear can swap sides, so rear < front no longer means empty. That is why a keeps the element count separately. The details start in What is a circular queue?.

6Comparison

ApproachDequeueFreed cellNote
Linear queueO(1)Never used againSays full while there is room
Reset when emptyO(1)Comes back only when the queue empties completelyPartial fix
Shift leftO(n)Used again at onceSlow for a large queue
Circular queueO(1)Used again at onceKeeps 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?

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?

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.