---
title: "Circular queue algorithm with conditions"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:circular-queue-algorithm-conditions"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/circular-queue-algorithm-conditions
site: BCT Engineering
---

# Circular queue algorithm with conditions

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2070 Magh, 5 marks
- Video: [Introduction to Circular Queues](https://www.youtube.com/watch?v=ihEmEcO2Hx8) — Lalitha Natraj
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/circular-queue-algorithm-conditions
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Write an algorithm to implement circular queue with the condition for insertion, deletion, queue full and queue empty.

## Short answer

A circular queue uses an array, empty when front==-1, full when (rear+1)%N==front. Insertion: if not full increment rear modulo N and store. Deletion: if not empty retrieve front, increment front modulo N, reset both to -1 if last element removed.

## Answer

A circular queue stores items in an array `CQ[0..N-1]` and uses `(index + 1) % N` so that the position after N − 1 is 0.

#### Conditions

| Condition | Test |
|---|---|
| Initially empty | `front = rear = -1` |
| Queue empty | `front == -1` |
| Queue full | `(rear + 1) % N == front` |
| Insertion allowed | queue not full |
| Deletion allowed | queue not empty |
| Exactly one element | `front == rear` (after deleting it, reset both to −1) |

#### Algorithm for insertion

```text
ENQUEUE(CQ, x)
1. if (rear + 1) % N == front:  print "Queue full"; return
2. if front == -1:  front = 0;  rear = 0
   else             rear = (rear + 1) % N
3. CQ[rear] = x
```

#### Algorithm for deletion

```text
DEQUEUE(CQ)
1. if front == -1:  print "Queue empty"; return
2. x = CQ[front]
3. if front == rear:  front = -1;  rear = -1
   else               front = (front + 1) % N
4. return x
```

#### Example (N = 4)

| Operation | Array contents (index 0 → N-1) | front | rear |
|---|---|---|---|
| enqueue(1) | 1 _ _ _ | 0 | 0 |
| enqueue(2) | 1 2 _ _ | 0 | 1 |
| enqueue(3) | 1 2 3 _ | 0 | 2 |
| enqueue(4) | 1 2 3 4 | 0 | 3 |
| enqueue(5): (rear+1)%4 = 0 = front → Queue overflow | 1 2 3 4 | 0 | 3 |
| dequeue() returns 1 | _ 2 3 4 | 1 | 3 |
| enqueue(5) | 5 2 3 4 | 1 | 0 |
| dequeue() returns 2 | 5 _ 3 4 | 2 | 0 |
| dequeue() returns 3 | 5 _ _ 4 | 3 | 0 |
| dequeue() returns 4 | 5 _ _ _ | 0 | 0 |
| dequeue() returns 5 | _ _ _ _ | -1 | -1 |
| dequeue(): Queue underflow | _ _ _ _ | -1 | -1 |

All N cells can be used, and the wrap-around of `rear` shows the circular behaviour.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. A circular queue is empty when:
   A. (rear+1)%N == front
   B. front == -1
   C. rear == N-1
   D. front == 0
   Answer: B. front = rear = -1 is the empty state. (Key point: Empty condition)

2. What is the full test for the circular queue?
   A. front == rear
   B. front == -1
   C. (rear + 1) % N == front
   D. rear == front + 1
   Answer: C. If the cell after rear is front, the ring is full. (Key point: Full condition)

3. When inserting into an empty circular queue, what is done?
   A. rear = (rear+1)%N only
   B. front = 0 and rear = 0
   C. front = -1
   D. front = N-1
   Answer: B. The first element sets both front and rear to 0 before storing. (Key point: First insertion)

4. Why are front and rear reset to -1 when the last element is deleted?
   A. To mark the queue empty again
   B. To mark it full
   C. To free memory
   D. To reverse the queue
   Answer: A. front == -1 is the empty test, so resetting restores that state. (Key point: Reset on last deletion)

5. In the N=4 example, why can enqueue(5) succeed after dequeue()?
   A. Array grows
   B. rear wraps to index 0, which was freed
   C. front resets to -1
   D. Elements are shifted left
   Answer: B. The wrap-around reuses the freed cell at index 0. (Key point: Reuse of space)

## Related in Data Structure and Algorithm

- [Insertion and deletion in array list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-insert-delete) — Data Structure and Algorithm, chapter 3
- [Enqueue and dequeue in circular queue](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-queue-insert-delete) — Data Structure and Algorithm, chapter 2, asked 2×
- [Circular queue using linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-queue-linked-list) — Data Structure and Algorithm, chapter 4
- [Circular queue vs linear queue](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-vs-linear-queue) — Data Structure and Algorithm, chapter 2
- [Circular queue with display operation](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-queue-enqueue-dequeue-display) — Data Structure and Algorithm, chapter 2
- [Linear and circular queue operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alinear-circular-queue-algorithms) — Data Structure and Algorithm, chapter 2

## Related in other subjects

- [Circular sewer just full, 1.6 m³/s](https://www.bctengineer.com/question.md?key=semester_6%2Fsanitary_engineering%3Asewer-just-full-1p6-slope-1-150) — Sanitary Engineering, chapter 3
- [Race condition example; Peterson's algorithm](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arace-condition-example-peterson) — Operating System, chapter 3
- [Insertion loss design of given LC ladder filter](https://www.bctengineer.com/question.md?key=semester_7%2Frf_and_microwave_engineering%3Afilter-ladder-insertion-loss-microstrip) — RF and Microwave Engineering, chapter 6
