5.2Circular Queue
Enqueue: wrapping around with mod
Enqueue on a circular queue is almost the same as on a linear one. The only difference is how rear moves: when rear reaches the end of the array, mod sends it back to 0. That way the front cells wasted by a linear queue are used again.
- Why a full queue is detected with
count, not withrear - How
(rear + 1) % CAPACITYwrapsrearback to the start - The four steps of enqueue and what happens on a full queue
1Idea
The new element goes into the cell right after the last element. In a linear queue that is rear + 1. A circular queue uses (rear + 1) % CAPACITY instead. If rear is 4 and the capacity is 5, (4 + 1) % 5 = 0: the next cell is the start of the array.
A full queue can no longer be detected with rear == CAPACITY - 1. While rear sits in the last cell, the start of the array may be empty. So the number of elements in the queue is kept in a count field, and the queue is full when count == CAPACITY.
The order is: check for a full queue, move rear forward, write the value to items[rear], add one to count. No element is shifted and front is not touched.
Starting rear at -1 makes the first enqueue work too: (-1 + 1) % 5 = 0. The first element goes into cell 0.
2Step by step
The drawing shows a queue with capacity 5 holding 7 and 9, both at the end of the array. First 5 is added and rear wraps around. Then the queue is filled and one more enqueue is tried.
- Start.
frontis 3,rearis 4,countis 2. The queue is 7, 9. The three front cells are empty, butrearis in the last cell of the array. isFullis checked:countis 2 and the capacity is 5. There is room, so we go on.rearmoves forward:(4 + 1) % 5 = 0.rearhas wrapped around to the start of the array.- The value is written to
items[0]. 5 is now at the rear of the queue. countbecomes 3. The queue order is 7, 9, 5: 5 sits on the left of the array, but it is last in line.- 8 and 2 were added too.
rearis 2 andcountis 5: every cell is used. - enqueue(6) is tried. Since
count == 5, the queue is full and 6 is not written. C returns false, Java and Python throw an error.
#define CAPACITY 5 struct Queue { int items[CAPACITY]; int front; int rear; int count;}; void create(struct Queue* q) { q->front = 0; q->rear = -1; q->count = 0;} bool isFull(struct Queue* q) { return q->count == CAPACITY;} bool enqueue(struct Queue* q, int value) { if (isFull(q)) { return false; // queue is full } q->rear = (q->rear + 1) % CAPACITY; q->items[q->rear] = value; q->count++; return true;}class CircularQueue { int[] items; int front = 0; int rear = -1; int count = 0; CircularQueue(int capacity) { items = new int[capacity]; } boolean isFull() { return count == items.length; } void enqueue(int value) { if (isFull()) { throw new IllegalStateException("queue is full"); } rear = (rear + 1) % items.length; items[rear] = value; count++; }}class CircularQueue: def __init__(self, capacity): self.items = [None] * capacity self.front = 0 self.rear = -1 self.count = 0 def is_full(self): return self.count == len(self.items) def enqueue(self, value): if self.is_full(): raise IndexError("queue is full") self.rear = (self.rear + 1) % len(self.items) self.items[self.rear] = value self.count += 1The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
One comparison, one mod, one write and one increment. The number of elements in the queue does not matter.
The room is already set aside in the array; enqueue asks for no new memory.
Enqueue on a linear queue is O(1) as well, but once the array hits its end it cannot take an element even with empty cells at the front. A circular queue is just as fast and uses every cell.
4Common mistake
The most common mistake is forgetting the mod or putting it in the wrong place:
q->rear = q->rear + 1 % CAPACITY; // wrong: 1 % 5 is computed first% binds tighter than +. This line means rear + (1 % 5), that is rear + 1, and once rear reaches 5 the write goes past the end of the array. The parentheses are required: (q->rear + 1) % CAPACITY. Another mistake is checking for a full queue with rear == CAPACITY - 1; that is the linear queue check and it calls the queue full while there is room at the start.
5Try it
A circular queue with capacity 6 has front = 4, rear = 5, count = 2. After enqueue(3), what is rear and which cell holds 3?
The answer is B. count is 2 and the capacity is 6, so there is room. rear = (5 + 1) % 6 = 0. 3 is written to the start of the array, items[0]. There is no cell 6.
Show the answer
The answer is B. count is 2 and the capacity is 6, so there is room. rear = (5 + 1) % 6 = 0. 3 is written to the start of the array, items[0]. There is no cell 6.
Why is a full circular queue not detected with rear == CAPACITY - 1?
The answer is B. Dequeue moves front forward and frees the front cells. Even with rear at the end, it can wrap around and write into them. What tells you the queue is full is the element count: count == CAPACITY.
Show the answer
The answer is B. Dequeue moves front forward and frees the front cells. Even with rear at the end, it can wrap around and write into them. What tells you the queue is full is the element count: count == CAPACITY.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.