---
title: "Linear and circular queue operations"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:linear-circular-queue-algorithms"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/linear-circular-queue-algorithms
site: BCT Engineering
---

# Linear and circular queue operations

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

- Asked in: 2069 Bhadra, 5 marks
- 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/linear-circular-queue-algorithms
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define linear and circular queue. Write down algorithms for basic operations that can be performed over linear and circular queues.

## Short answer

Linear queue moves front and rear forward; after dequeues free cells are unused, and overflow occurs when rear reaches N‑1. Circular queue wraps pointers modulo N, reusing free cells. Example: after two dequeues, a linear queue overflows on enqueue; circular wraps and succeeds.

## 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).

- A **linear queue** is stored in an array in a straight line; `front` and `rear` only move forward, so cells freed by deletion are not reused.
- A **circular queue** treats the array as a ring (last cell connected to first); `front` and `rear` wrap around using `% N`, so freed cells are reused.

#### Linear queue (array Q[0..N-1], front = 0, rear = -1)

```text
ENQUEUE(x)
1. if rear == N - 1:  print "Queue overflow"; return
2. rear = rear + 1
3. Q[rear] = x

DEQUEUE()
1. if front > rear:  print "Queue underflow"; return
2. x = Q[front]
3. front = front + 1
4. return x
```

#### Circular queue (array CQ[0..N-1], front = rear = -1)

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

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

#### Example showing the difference (N = 5; insert 10..50, delete two, insert 60)

Linear queue:

| 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 |
| dequeue() returns 10 | _ 20 30 40 50 | 1 | 4 |
| dequeue() returns 20 | _ _ 30 40 50 | 2 | 4 |
| enqueue(60): rear = 4 → Queue overflow | _ _ 30 40 50 | 2 | 4 |

Circular queue:

| 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 |
| 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 |

## Check yourself

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

1. In a queue, insertion and deletion occur at:
   A. Rear and front respectively
   B. Front and rear respectively
   C. Both at front
   D. Both at rear
   Answer: A. Insert at rear, delete at front, giving FIFO. (Key point: FIFO)

2. What is the drawback of a linear array queue?
   A. Cells freed by deletion are not reused
   B. It cannot be empty
   C. It is always full
   D. rear moves backward
   Answer: A. front and rear only move forward so freed cells at the start are wasted. (Key point: Linear queue drawback)

3. In the N=5 example, enqueue(60) after two deletions gives:
   A. Overflow in both queues
   B. Overflow in linear, success in circular
   C. Success in linear, overflow in circular
   D. Success in both
   Answer: B. Linear has rear = 4 = N-1, while circular wraps rear to index 0. (Key point: Circular advantage)

4. What is the overflow test in the linear ENQUEUE?
   A. rear == N - 1
   B. (rear+1)%N == front
   C. front == -1
   D. front > rear
   Answer: A. A linear queue is full when rear reaches the last index. (Key point: Linear overflow test)

5. What is the underflow test in the linear DEQUEUE?
   A. front == -1
   B. front > rear
   C. rear == N-1
   D. front == 0
   Answer: B. When front passes rear, no elements remain. (Key point: Linear underflow test)

## Related in Data Structure and Algorithm

- [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
- [Stack definition and basic operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-operations-algorithms) — 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
- [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 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 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

## Related in other subjects

- [Basic forging operations](https://www.bctengineer.com/question.md?key=semester_2%2Fworkshop_technology%3Aforging-operations-explained) — Workshop Technology, chapter 9
- [Basic lathe operations](https://www.bctengineer.com/question.md?key=semester_2%2Fworkshop_technology%3Alathe-operations-explained) — Workshop Technology, chapter 6
- [Basic requirements of linear programming](https://www.bctengineer.com/question.md?key=semester_7%2Fproject_engineering%3Alinear-programming-requirements) — Project Engineering, chapter 3
- [Linear convolution via circular convolution](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Alinear-conv-via-circular) — Digital Signal Analysis and Processing, chapter 7
