---
title: "Queue; enqueue and dequeue operations"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:queue-enqueue-dequeue"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/queue-enqueue-dequeue
site: BCT Engineering
---

# Queue; enqueue and dequeue operations

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

- Asked in: 2074 Bhadra, 1+4 marks; 2070 Bhadra, 1+4 marks (asked 2 times)
- Video: [Data structures: Array implementation of Queue](https://www.youtube.com/watch?v=okr-XE8yTO8) — mycodeschool
- 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/queue-enqueue-dequeue
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define queue. Explain enqueue and dequeue operation with example.

## Short answer

A queue is a FIFO linear structure with insertion at the rear and deletion at the front. Enqueue adds an element by incrementing rear and placing the element; dequeue removes and returns the front element, then increments front. Example: starting empty, enqueue 10,20,30; dequeue returns 10, then front moves to index 1.

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

#### Enqueue (insert at rear) and dequeue (delete from front)

```text
Algorithm ENQUEUE(Q, N, x)        /* array Q[0..N-1], front = 0, rear = -1 initially */
1. if rear == N - 1:
       print "Queue overflow"; return
2. rear = rear + 1
3. Q[rear] = x
```
```text
Algorithm DEQUEUE(Q)
1. if front > rear:
       print "Queue underflow"; return
2. x = Q[front]
3. front = front + 1
4. return x
```

#### 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 |
| dequeue() returns 10 | _ 20 30 _ _ | 1 | 2 |
| enqueue(40) | _ 20 30 40 _ | 1 | 3 |
| dequeue() returns 20 | _ _ 30 40 _ | 2 | 3 |

`front` moves right on every dequeue and `rear` moves right on every enqueue; the queue is empty when `front > rear` and full when `rear = N - 1`.

## Check yourself

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

1. A queue inserts at the ___ and deletes from the ___.
   A. front; rear
   B. top; top
   C. rear; front
   D. front; front
   Answer: C. First in, first out. (Key point: FIFO)

2. In the array queue with N elements, when is overflow reported on enqueue?
   A. rear == N - 1
   B. front > rear
   C. front == N - 1
   D. rear == 0
   Answer: A. No cell remains after the last index. (Key point: Overflow condition)

3. What changes after dequeue()?
   A. front moves right by one
   B. rear moves right
   C. front moves left
   D. rear resets to -1
   Answer: A. x = Q[front]; front = front + 1. (Key point: Dequeue effect)

4. The linear queue is empty when:
   A. front == rear
   B. rear == N - 1
   C. front == 0
   D. front > rear
   Answer: D. With front = 0 and rear = -1 initially, front > rear means no elements. (Key point: Empty condition)

## Related in Data Structure and Algorithm

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