5.1Queue with an Array
Dequeue: removing from the front
dequeue removes the element at the front of a queue and hands back its value. The first element in is the first one out; this is called FIFO. In an array queue it takes three short steps: check whether the queue is empty, read the value at front, add one to front. No element is shifted.
- Why dequeue starts with an empty check
- Why the read happens before front is increased
- Why the freed cell is not cleared and what happens when the queue runs empty
1Idea
front is the index of the element whose turn comes first. Dequeue reads items[front] first and then adds one to front. Now the next element is at the front. The removed value stays in its cell, but since it is left of front it is no longer part of the queue. There is no need to clear it; it may be written over later.
Shifting the elements to the left is another way: then front always stays at 0. But every dequeue moves all elements one cell, and the operation becomes O(n). Here there is no shifting, so the operation is O(1). The price is that cells at the start go to waste; you see this in the wasted cells lesson.
If the queue is empty, there is nothing to remove. In C the function returns false and leaves value alone. Java throws IllegalStateException and Python raises IndexError. Once the last element leaves, front passes rear: rear < front holds and isEmpty returns true.
The C version hands the value out through a pointer (int* value), because the return value is used to report success.
2Step by step
In the drawing, one element is removed from a queue holding 3, 8 and 5. Then the queue is emptied and one more dequeue is tried. Faded cells are no longer part of the queue.
- The start. The queue holds 3, 8 and 5,
frontis 0 andrearis 2. dequeue is called. isEmptyis checked:rear < front, that is 2 < 0, is false. The queue is not empty, so it goes on.items[front]is read:valuebecomes 3. 3 went in first, and it comes out first.frontis increased by one and becomes 1. Cell 0 is now outside the queue; 3 is still there but no longer counts.- The function hands back 3. The front element is now 8.
rearnever changed. - Two more dequeues: 8 and 5 left.
frontis 3 andrearis 2.fronthas passedrear, so the queue is empty. - One more dequeue is tried.
isEmptyreturns true: C returns false, Java and Python throw an error.frontstays at 3.
#define CAPACITY 5 struct Queue { int items[CAPACITY]; int front; int rear;}; void create(struct Queue* q) { q->front = 0; q->rear = -1;} bool isEmpty(struct Queue* q) { return q->rear < q->front;} bool dequeue(struct Queue* q, int* value) { if (isEmpty(q)) { return false; // queue is empty } *value = q->items[q->front]; q->front++; return true;}class ArrayQueue { int[] items; int front = 0; int rear = -1; ArrayQueue(int capacity) { items = new int[capacity]; } boolean isEmpty() { return rear < front; } int dequeue() { if (isEmpty()) { throw new IllegalStateException("queue is empty"); } int value = items[front]; front++; return value; }}class ArrayQueue: def __init__(self, capacity): self.items = [None] * capacity self.front = 0 self.rear = -1 def is_empty(self): return self.rear < self.front def dequeue(self): if self.is_empty(): raise IndexError("queue is empty") value = self.items[self.front] self.front += 1 return valueThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
One comparison, one read and one increment. The length of the queue does not matter.
Only one variable that holds the removed value is used.
It stays O(1) because nothing is shifted. The free cells that this costs are won back by dequeue in a circular queue.
4Common mistake
A common mistake is increasing front before reading the value:
q->front++; // wrong: moves on first*value = q->items[q->front];This code hands back the element after the front one, not the front one. The front element is lost without ever being read. With a single element in the queue, it reads a cell past rear that is not part of the queue. The right order is to read first, then front++.
5Try it
4, 7 and 2 were enqueued in that order. Which values do two dequeues give?
The answer is B. A queue is FIFO: first in, first out. 4 was added first, then 7. On a stack the answer would be 2 and 7.
Show the answer
The answer is B. A queue is FIFO: first in, first out. 4 was added first, then 7. On a stack the answer would be 2 and 7.
With front = 2 and rear = 2, what happens after a dequeue?
The answer is A. The queue holds one element. Dequeue reads it and sets front to 3. Since rear < front, the queue is now empty; in this code the indexes do not go back to the start.
Show the answer
The answer is A. The queue holds one element. Dequeue reads it and sets front to 3. Since rear < front, the queue is now empty; in this code the indexes do not go back to the start.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.