---
title: "Enqueue and dequeue in circular queue"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:circular-queue-insert-delete"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/circular-queue-insert-delete
site: BCT Engineering
---

# Enqueue and dequeue in circular queue

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

- Asked in: 2072 Asoj, 1+4 marks; 2071 Bhadra, 5 marks (asked 2 times)
- Video: [Circular Queue Implementation - Array](https://www.youtube.com/watch?v=8sjFA-IX-Ww) — Blue Tree Code
- 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-insert-delete
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define a queue. Explain enqueue and dequeue (insertion and deletion) operation in circular queue with example.

## Short answer

A circular queue uses array modulo N to wrap around, reusing vacant front slots. Enqueue: if (rear+1)%N == front then overflow, else set front=0 on first element, adjust rear and insert. Dequeue: remove at front, if last element reset both pointers, else front = (front+1)%N. Example shows enqueues 10..50, dequeues, then enqueue 60,70 wrap around.

## Answer

A **queue** is a linear data structure in which insertion takes place at one end, the **rear**, and deletion at the other end, the **front**. The element inserted first is removed first, so it follows **FIFO** (First In First Out).

In a **circular queue** the last position is joined to the first, so the array is treated as a ring and positions freed at the front are reused. Indices wrap around using the modulus operator.

#### Enqueue and dequeue

```text
Algorithm ENQUEUE(CQ, N, x)      /* front = rear = -1 initially */
1. if (rear + 1) % N == front:
       print "Queue overflow"; return
2. if front == -1:               /* first element */
       front = rear = 0
   else
       rear = (rear + 1) % N
3. CQ[rear] = x

Algorithm DEQUEUE(CQ, N)
1. if front == -1:
       print "Queue underflow"; return
2. x = CQ[front]
3. if front == rear:             /* last element removed */
       front = rear = -1
   else
       front = (front + 1) % N
4. return x
```

- Full condition: `(rear + 1) % N == front`
- Empty condition: `front == -1`

#### Example (N = 5)

| Operation | Array contents (index 0 → N-1) | front | rear |
|---|---|---|---|
| enqueue(10) | 10 _ _ _ _ | 0 | 0 |
| enqueue(20) | 10 20 _ _ _ | 0 | 1 |
| enqueue(30) | 10 20 30 _ _ | 0 | 2 |
| enqueue(40) | 10 20 30 40 _ | 0 | 3 |
| enqueue(50) | 10 20 30 40 50 | 0 | 4 |
| enqueue(60): (rear+1)%5 = 0 = front → Queue overflow | 10 20 30 40 50 | 0 | 4 |
| dequeue() returns 10 | _ 20 30 40 50 | 1 | 4 |
| dequeue() returns 20 | _ _ 30 40 50 | 2 | 4 |
| enqueue(60) | 60 _ 30 40 50 | 2 | 0 |
| enqueue(70) | 60 70 30 40 50 | 2 | 1 |
| enqueue(80): (rear+1)%5 = 2 = front → Queue overflow | 60 70 30 40 50 | 2 | 1 |
| dequeue() returns 30 | 60 70 _ 40 50 | 3 | 1 |

After two dequeues, `enqueue(60)` and `enqueue(70)` succeed by wrapping `rear` to index 0 and 1; a linear queue would have reported overflow.

## Check yourself

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

1. In a circular queue, the full condition is:
   A. rear == N - 1
   B. (rear + 1) % N == front
   C. front == -1
   D. front == rear
   Answer: B. The next position of rear would collide with front. (Key point: Full condition)

2. How does rear move on enqueue?
   A. rear = rear + 1
   B. rear = N - 1
   C. rear = (rear + 1) % N
   D. rear = front
   Answer: C. The modulus wraps rear back to 0. (Key point: Wrap-around)

3. What happens in dequeue when front == rear?
   A. front = front + 1 only
   B. front = rear = -1 (queue becomes empty)
   C. Overflow is reported
   D. rear = N - 1
   Answer: B. The last element has been removed. (Key point: Last element removed)

4. With N=5, 10..50 enqueued then two dequeues, enqueue(60) stores 60 at:
   A. Index 5
   B. Overflow is reported
   C. Index 2
   D. Index 0, rear = 0
   Answer: D. (4+1)%5 = 0, and front is 2, so it is not full. (Key point: Example result)

## Related in Data Structure and Algorithm

- [Queue; enqueue and dequeue operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aqueue-enqueue-dequeue) — Data Structure and Algorithm, chapter 2, asked 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
- [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
- [Circular queue algorithm with conditions](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-queue-algorithm-conditions) — Data Structure and Algorithm, chapter 2
- [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
- [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
