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

Creating an array queue

A is a structure where elements go in at one end and come out at the other. Built on an array, it needs two indexes instead of a stack's one: one marks the front element, the other marks the rear. This lesson covers the structure and two checks: is the queue empty, is it full? The and lessons that follow rely on these checks.

What you will learn
  • Which three fields make up an array queue
  • Why starts at 0 and at -1
  • How you tell an empty or a full queue from these two indexes
5 minTime O(1)Memory O(1)C · Java · Python

1Idea

The structure has three fields: the items array that holds the elements, and . front is the of the element whose turn comes first, and rear is the index of the element added last. A new element is written one cell to the right of rear; the element that leaves is the one at front.

An empty queue has front at 0 and rear at -1. The first moves rear to 0, and both indexes point at the same cell: a one-element queue. So the empty check is rear < front. The number of elements is always rear - front + 1.

The full check is rear == CAPACITY - 1: once rear reaches the last cell there is no room on the right. There is a trap here: if front has moved on, there may be free cells at the start of the array, but this queue cannot use them. The last step of the drawing shows it; the fix is in wasted cells.

A stack needed one index because adding and removing happen at the same end. A queue has two ends, so it keeps two indexes.

2Step by step

The drawing shows an array of 5 cells. The boxes on the right show the values of front and rear. The label above a cell marks front, the label below marks rear.

0123430815263940front2front-1rear2rear4rearfrontrear
  1. The queue is created. Space is set aside for the array, front is set to 0 and rear to -1. Whatever is in the cells does not matter; they are not part of the queue.
  2. isEmpty: rear < front, that is -1 < 0, is true. The queue is empty, so the result is true.
  3. isFull: rear is not 4, so the queue is not full. The result is false.
  4. After three calls: 3, 8 and 5 were written, front is 0 and rear is 2. isEmpty now returns false.
  5. Two calls and two more enqueues: 3 and 8 left, 6 and 9 came in. front is 2 and rear is 4. isFull returns true, even though cells 0 and 1 are not in use.

#define CAPACITY 5 struct Queue {    int items[CAPACITY];    int front;  // index of the front element    int rear;   // index of the rear element}; void create(struct Queue* q) {    q->front = 0;    q->rear = -1;  // empty queue} bool isEmpty(struct Queue* q) {    return q->rear < q->front;} bool isFull(struct Queue* q) {    return q->rear == CAPACITY - 1;}
class ArrayQueue {    int[] items;    int front = 0;   // index of the front element    int rear = -1;   // index of the rear element     ArrayQueue(int capacity) {        items = new int[capacity];    }     boolean isEmpty() {        return rear < front;    }     boolean isFull() {        return rear == items.length - 1;    }}
class ArrayQueue:    def __init__(self, capacity):        self.items = [None] * capacity        self.front = 0   # index of the front element        self.rear = -1   # index of the rear element     def is_empty(self):        return self.rear < self.front     def is_full(self):        return self.rear == len(self.items) - 1

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

3Complexity

Time
O(1)

Creating is two assignments and each check is one comparison. None of them depends on the number of elements.

Memory
O(1)

The checks use no extra space. The array itself takes room for the full capacity, that is O(k).

This simple queue is called a linear queue. The version that also reuses the cells freed at the front is the circular queue.

4Common mistake

A common mistake is checking for an empty queue with rear == -1:

return q->rear == -1;  // wrong: true only if nothing was ever added

This check is only right for a freshly created queue. After elements are added and all of them removed, rear may be 2 and front 3; the queue is empty, but rear == -1 returns false and a dequeue runs on an empty queue. The right check compares the two indexes: rear < front.

5Try it

An array queue with capacity 5 has front = 1 and rear = 3. How many elements does it hold?

Show the answer

The answer is B. The elements sit in the cells from front to rear: 1, 2 and 3. The count is rear - front + 1 = 3.

With front = 2 and rear = 4, what does isFull return?

Show the answer

The answer is B. A linear queue can only write to the right of rear. When rear is 4 there is no room left; the free cells at the start cannot be used. The queue actually holds 3 elements.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.