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

Dequeue: removing from the front

removes the element at the front of a and hands back its value. The first element in is the first one out; this is called . In an array queue it takes three short steps: check whether the queue is empty, read the value at , add one to front. No element is shifted.

What you will learn
  • 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
5 minTime O(1)Memory O(1)C · Java · Python

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 . Here there is no shifting, so the operation is . 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.

308152340front1front3front2rear3value5valuefrontrear
  1. The start. The queue holds 3, 8 and 5, front is 0 and rear is 2. dequeue is called.
  2. isEmpty is checked: rear < front, that is 2 < 0, is false. The queue is not empty, so it goes on.
  3. items[front] is read: value becomes 3. 3 went in first, and it comes out first.
  4. front is increased by one and becomes 1. Cell 0 is now outside the queue; 3 is still there but no longer counts.
  5. The function hands back 3. The front element is now 8. rear never changed.
  6. Two more dequeues: 8 and 5 left. front is 3 and rear is 2. front has passed rear, so the queue is empty.
  7. One more dequeue is tried. isEmpty returns true: C returns false, Java and Python throw an error. front stays 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 value

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

One comparison, one read and one increment. The length of the queue does not matter.

Memory
O(1)

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?

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?

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.