5.1Queue with an Array
Creating an array queue
A queue 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 enqueue and dequeue lessons that follow rely on these checks.
1Idea
The structure has three fields: the items array that holds the elements, front and rear. front is the index 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 enqueue 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.
- The queue is created. Space is set aside for the array,
frontis set to 0 andrearto -1. Whatever is in the cells does not matter; they are not part of the queue. isEmpty:rear < front, that is -1 < 0, is true. The queue is empty, so the result is true.isFull:rearis not 4, so the queue is not full. The result is false.- After three enqueue calls: 3, 8 and 5 were written,
frontis 0 andrearis 2.isEmptynow returns false. - Two dequeue calls and two more enqueues: 3 and 8 left, 6 and 9 came in.
frontis 2 andrearis 4.isFullreturns 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) - 1The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Creating is two assignments and each check is one comparison. None of them depends on the number of elements.
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 addedThis 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?
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.
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?
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.
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.