5.2Circular Queue
What is a circular queue?
In a linear queue, the cells at the start of the array go to waste as front moves forward. A circular queue fixes this with a simple idea: it acts as if the end of the array were joined to its start. When rear reaches the last cell, the new element goes into cell 0. This lesson covers the idea, the mod operation and how to find the number of elements.
- Which problem of the linear queue the circular queue solves
- How indexes wrap around with
% CAPACITY - The element count formula and why a full queue and an empty one can look alike
1The problem: no room on the right, empty cells on the left
Picture a linear queue with capacity 5. You add five elements and remove three. front is at 3 and rear is at 4. The queue holds two elements, yet isFull returns true because rear is in the last cell. Cells 0, 1 and 2 are empty, but the queue cannot use them.
2The idea: join the end of the array to its start
Think of the array as a ring: cell 0 comes after cell 4. If rear is at 4, the new element goes into cell 0. If front is at 4, it moves to 0 after a removal. In memory the array is still a straight row; the ring is only how we compute the indexes.
To read the queue you start at front, move right, go back to the start when you hit the end, and stop at rear. In the drawing the queue is in cells 3, 4, 0 and 1, in that order.
3Wrapping around with mod
You could write the wrap with an if: when the index reaches CAPACITY, set it to 0. The shorter way is mod, the remainder operation. With CAPACITY 5, (i + 1) % 5 gives one more than i for 0, 1, 2 and 3; for 4 it gives 5 % 5 = 0.
q->rear = (q->rear + 1) % CAPACITY; // addingq->front = (q->front + 1) % CAPACITY; // removingThe element at position i in the queue is found the same way: items[(front + i) % CAPACITY]. The loop below prints the queue starting from the front element:
for (int i = 0; i < q->count; i++) { printf("%d ", q->items[(q->front + i) % CAPACITY]);}4Let's trace a sequence of operations
Start with an empty circular queue of capacity 5. Besides front and rear, the code in this module keeps the number of elements in a count field. In the table, cells that are not part of the queue are shown as dots.
| Operation | Array (0..4) | front | rear | count |
|---|---|---|---|---|
| create | · · · · · | 0 | -1 | 0 |
| enqueue(10), (20), (30) | 10 20 30 · · | 0 | 2 | 3 |
| dequeue() → 10 | · 20 30 · · | 1 | 2 | 2 |
| dequeue() → 20 | · · 30 · · | 2 | 2 | 1 |
| enqueue(40), (50) | · · 30 40 50 | 2 | 4 | 3 |
| enqueue(60) | 60 · 30 40 50 | 2 | 0 | 4 |
| enqueue(70) | 60 70 30 40 50 | 2 | 1 | 5 |
| enqueue(80) | full, not added | 2 | 1 | 5 |
When 60 was added, rear wrapped from 4 to 0. A linear queue would have said full at this point. The circular queue used the two cells at the start as well and found room for all five elements. The queue order is 30, 40, 50, 60, 70.
5How many elements are there?
In a linear queue the number of elements was rear - front + 1. In a circular queue rear can be smaller than front after a wrap, and that expression goes negative. The fixed formula is:
int n = (q->rear - q->front + 1 + CAPACITY) % CAPACITY;Adding CAPACITY keeps the result from going negative; in C and Java % gives a negative result for a negative number. For the queue in the second drawing it gives (1 - 3 + 1 + 5) % 5 = 4.
This formula has a gap. In the full queue at the end of the table, front is 2, rear is 1 and the formula gives (1 - 2 + 1 + 5) % 5 = 0. Remove five elements and the queue is empty, yet front and rear are 2 and 1 again. A full queue and an empty one look the same. That is why count is kept; the other fixes are in the Full or empty? lesson.
6What this module covers
- Enqueue: moving
rearwith mod, then writing. - Dequeue: reading the front element, then moving
front. - Full or empty?: the
count, one empty cell and flag methods.
7Try it
A circular queue with capacity 6 has rear = 5 and is not full. What is rear after an enqueue?
The answer is B. rear = (5 + 1) % 6 = 0. There is no cell 6; rear wraps around to the start and the new element goes into items[0].
Show the answer
The answer is B. rear = (5 + 1) % 6 = 0. There is no cell 6; rear wraps around to the start and the new element goes into items[0].
A circular queue with capacity 5 has front = 4 and rear = 1. The queue is not empty. How many elements does it hold?
The answer is B. (1 - 4 + 1 + 5) % 5 = 3. The elements are in cells 4, 0 and 1. rear - front + 1 would give -2, because it ignores the wrap.
Show the answer
The answer is B. (1 - 4 + 1 + 5) % 5 = 3. The elements are in cells 4, 0 and 1. rear - front + 1 would give -2, because it ignores the wrap.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.