5.2Circular Queue
Dequeue: moving front forward
Dequeue removes the element at the front of the queue and returns it. In a circular queue, front moves forward just like rear does, and when it reaches the end of the array mod sends it back to the start. In this lesson you see the steps of dequeue and how front wraps around.
- Why dequeue looks at
countfirst - Why the value is read before
frontmoves - How
frontwraps around with(front + 1) % CAPACITY
1Idea
The front element is in items[front]. Dequeue copies that value into a variable, then moves front forward one cell: front = (front + 1) % CAPACITY. If front is in the last cell, the expression gives 0 and front goes to the start of the array. Finally count goes down by one.
If the queue is empty (count == 0), there is no element to remove. That is why dequeue starts with the isEmpty check. In C the function returns false and hands back the value through int* value; Java throws an IllegalStateException and Python raises an IndexError.
Dequeue does not touch rear. When the queue becomes empty, front and rear end up side by side: front is the cell right after rear. We tell that the queue is empty by looking at count, not at these two. The Full or empty? lesson explains why.
Dequeue does not erase the cell. The removed value stays in the array but no longer belongs to the queue; once rear wraps around, an enqueue writes over it.
2Step by step
The drawing shows a queue with capacity 5 that has wrapped around: the order is 7, 9, 5, 8. First 7 is removed, then front wraps around as 9 leaves. The queue is emptied and one more dequeue is tried. Faded cells are not part of the queue.
- Start.
frontis 3,rearis 1,countis 4. 7 is at the front; 9, 5 and 8 wait behind it. isEmptyis checked:countis 4. The queue is not empty, so we go on.- The front value is read:
value = items[3], that is 7. frontmoves forward:(3 + 1) % 5 = 4. 7 is still in the cell but no longer in the queue; the new front element is 9.countbecomes 3 and 7 is returned.- One more dequeue: 9 is read and
front = (4 + 1) % 5 = 0.fronthas wrapped around to the start; the front element is now 5. - 5 and 8 were removed too.
countis 0,frontis 2,rearis 1. The queue is empty andfrontsits right afterrear. - dequeue() is tried. Since
count == 0, the queue is empty. C returns false, Java and Python throw an error. No field changes.
#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 isEmpty(struct Queue* q) { return q->count == 0;} bool dequeue(struct Queue* q, int* value) { if (isEmpty(q)) { return false; // queue is empty } *value = q->items[q->front]; q->front = (q->front + 1) % CAPACITY; 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 isEmpty() { return count == 0; } int dequeue() { if (isEmpty()) { throw new IllegalStateException("queue is empty"); } int value = items[front]; front = (front + 1) % items.length; count--; return value; }}class CircularQueue: def __init__(self, capacity): self.items = [None] * capacity self.front = 0 self.rear = -1 self.count = 0 def is_empty(self): return self.count == 0 def dequeue(self): if self.is_empty(): raise IndexError("queue is empty") value = self.items[self.front] self.front = (self.front + 1) % len(self.items) self.count -= 1 return valueThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
One comparison, one read, one mod and one decrement. The number of elements in the queue does not matter.
Only one variable is used, to hold the removed value.
In a queue that shifts its elements to the left, dequeue would be O(n). A circular queue leaves the elements where they are and only moves front.
4Common mistake
A common mistake is forgetting the mod when moving front:
*value = q->items[q->front];q->front++; // wrong: front can reach 5With front at 4 this line makes it 5. The next dequeue reads items[5]; in C that is past the end of the array, in Java it throws an ArrayIndexOutOfBoundsException. Another mistake is moving front first and reading after: then the element behind the front one is returned and the front element is lost.
5Try it
A circular queue with capacity 4 has front = 3, rear = 1, count = 3. What is front after dequeue()?
The answer is B. front = (3 + 1) % 4 = 0. There is no cell 4; front wraps around to the start of the array. The new front element is in items[0].
Show the answer
The answer is B. front = (3 + 1) % 4 = 0. There is no cell 4; front wraps around to the start of the array. The new front element is in items[0].
When a circular queue has been emptied completely, how are front and rear related?
The answer is B. Each dequeue moves front forward by one and leaves rear alone. When the last element leaves, front moves one past it, to the cell after rear. A full queue has the same relation, which is why count is kept.
Show the answer
The answer is B. Each dequeue moves front forward by one and leaves rear alone. When the last element leaves, front moves one past it, to the cell after rear. A full queue has the same relation, which is why count is kept.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.