algoritmauzmanı
Sign in
TREN
5.2Circular Queue
Lesson 5.2.4

Full or empty? Three ways to tell

In a , and alone are not enough: a full queue and an empty one can have the same front and rear values. This lesson shows where the problem comes from and three ways to solve it. The code in this module uses the first one, a count field.

What you will learn
  • Why a full queue and an empty one have the same front and rear values
  • How the count, one empty cell and flag methods work
  • How the three methods differ in memory and code
6 minTime O(1)Memory O(1)C · Java · Python

1Idea

In a full queue, rear sits right to the left of front: front == (rear + 1) % CAPACITY. When you empty the queue, each dequeue moves front forward by one, and front ends up one cell after rear again. The same equality holds for the empty queue. Looking only at front and rear, you cannot tell the two apart; you need one more piece of information.

1. Keeping the element count

A count field is added to the structure. Enqueue adds one to it and dequeue takes one away. Empty is count == 0 and full is count == CAPACITY. Every cell of the array is used and the checks are the easiest to read. The cost is updating one field on every operation. The code in this module uses this method.

2. Leaving one cell empty

No count is kept; instead the queue is never filled completely. In this method rear points to the next free cell, not to the last element, and front and rear both start at 0. An empty queue has front == rear. The queue counts as full when only one empty cell is left: (rear + 1) % CAPACITY == front.

// Here rear is the next free cell. At the start front = rear = 0.bool isEmpty(struct Queue* q) {    return q->front == q->rear;} bool isFull(struct Queue* q) {    return (q->rear + 1) % CAPACITY == q->front;}

No extra field is needed, but an array with capacity 5 holds at most 4 elements. Without count, the number of elements is computed with the formula.

3. Keeping a flag

A bool field is added that records whether the last operation was an enqueue or a dequeue. When front and rear reach the position that means full or empty, the flag decides: if the last operation added, the queue is full; if it removed, the queue is empty. Every cell is used, but every operation must also update the flag and the checks look at two conditions.

MethodExtra fieldCells usedEmptyFull
countone integerCAPACITYcount == 0count == CAPACITY
One empty cellnoneCAPACITY - 1front == rear(rear + 1) % CAPACITY == front
Flagone boolCAPACITYposition and last op removedposition and last op added

2Step by step

The drawing shows a full queue with capacity 5: the order is 30, 40, 50, 60, 70. Then five dequeues empty it. The boxes below show front, rear and count.

6007013024035042front1rear5count0countfrontrear
  1. A full queue. front is 2, rear is 1, count is 5. rear has wrapped around; the last element, 70, is in cell 1.
  2. Look at front and rear: (1 + 1) % 5 = 2, so front is one cell after rear.
  3. isFull: count == 5 is true. The queue is full.
  4. After five dequeues every element is gone. front passed 3, 4, 0 and 1 and came back to 2; rear did not change. count is 0.
  5. front is 2 and rear is 1: the same values as in the full queue. A check that looks only at these two would still think the queue is full.
  6. isEmpty: count == 0 is true. The queue is empty. The count field is what tells the two states apart.

#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 isFull(struct Queue* q) {    return q->count == CAPACITY;}
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;    }     boolean isFull() {        return count == items.length;    }}
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 is_full(self):        return self.count == len(self.items)

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

3Complexity

Time
O(1)

In all three methods a check is one or two comparisons. It does not depend on the number of elements.

Memory
O(1)

count or the flag adds a single field; the one empty cell method adds no field but gives up one cell.

Which one you pick is often a matter of taste. count also gives the number of elements for free, which is why the lesson code uses it.

4Common mistake

A common mistake is mixing two methods. The code starts with front = 0 and rear = -1 as in the count method, but takes the full check from the one empty cell method:

return (q->rear + 1) % CAPACITY == q->front;  // wrong: true when rear = -1

In a new queue (-1 + 1) % 5 = 0 and front is 0 as well. isFull says the empty queue is full and even the first enqueue is refused. Once you pick a method, the starting values, enqueue and both checks must all follow that method.

5Try it

With the one empty cell method, capacity is 5, front = 3 and rear = 2. What state is the queue in?

Show the answer

The answer is B. (2 + 1) % 5 = 3, which equals front. In this method that equality means full. The queue holds 4 elements, in cells 3, 4, 0 and 1. Cell 2 is left empty on purpose.

A queue that uses the count method has front = 2, rear = 1 and count = 0. What state is it in?

Show the answer

The answer is B. In this method count decides. front and rear can be in this position in a full queue and in an empty one; the queue in the drawing is in exactly this state after five dequeues.

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