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

# Circular queue with display operation

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

- Asked in: 2072 Magh, 5 marks
- Video: [Operations on Circular Queue |  Data Structures Tutorial](https://www.youtube.com/watch?v=9nEcSGdX5vY) — Naresh i Technologies
- 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-enqueue-dequeue-display
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Write an algorithm to design a data structure that provides enqueue, dequeue and display operations. The first two are the normal circular queue operations and the last one returns all elements in the circular queue without removing them from queue.

## Short answer

Use an array with wrap‑around via modulo. Enqueue inserts if (rear+1)%N != front; dequeue removes if front != -1. Display traverses from front to rear using i=(i+1)%N without altering pointers, printing elements in FIFO order.

## Answer

A circular queue is an array treated as a ring: after index N − 1 the next position is 0. Variables: array `CQ[0..N-1]`, `front = rear = -1` (empty).

#### Algorithms

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

DISPLAY()                      /* does not change front or rear */
1. if front == -1:  print "Queue empty"; return
2. i = front
3. loop:
       print CQ[i]
       if i == rear:  break
       i = (i + 1) % N
```

#### C implementation

```c
#include <stdio.h>
#define N 5
int cq[N], front = -1, rear = -1;

void enqueue(int x) {
    if ((rear + 1) % N == front) {               /* full */
        printf("Queue overflow\n"); return;
    }
    if (front == -1) front = rear = 0;           /* first element */
    else rear = (rear + 1) % N;
    cq[rear] = x;
}

int dequeue(void) {
    if (front == -1) {                           /* empty */
        printf("Queue underflow\n"); return -1;
    }
    int x = cq[front];
    if (front == rear) front = rear = -1;        /* last element */
    else front = (front + 1) % N;
    return x;
}
```

```c
void display(void) {
    if (front == -1) { printf("Queue empty\n"); return; }
    int i = front;
    while (1) {
        printf("%d ", cq[i]);
        if (i == rear) break;
        i = (i + 1) % N;
    }
    printf("\n");
}
```

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

`display()` at the end prints `30 40 50 60 70` (from `front = 2` wrapping to `rear = 1`) and leaves the queue unchanged. Each operation takes O(1) time except display, which is O(n).

## Check yourself

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

1. How does a circular queue move rear to the next position?
   A. rear = rear + 1
   B. rear = (rear + 1) % N
   C. rear = rear - 1
   D. rear = N - 1
   Answer: B. The modulo makes index N-1 wrap to 0. (Key point: Wrap-around)

2. A circular queue is full when:
   A. front == -1
   B. rear == N - 1
   C. (rear + 1) % N == front
   D. front == rear
   Answer: C. The next position of rear is front, so no free cell exists. (Key point: Full condition)

3. In DEQUEUE, when front == rear, what is done after reading the element?
   A. front is incremented
   B. front and rear are both reset to -1
   C. rear is decremented
   D. Nothing
   Answer: B. That was the only element so the queue becomes empty. (Key point: Last element in dequeue)

4. How does DISPLAY print the elements without removing them?
   A. Dequeues each one
   B. Starts at front and moves with (i+1)%N until i == rear
   C. Loops from 0 to N-1
   D. Starts at rear and goes backward to 0
   Answer: B. It traverses from front to rear with wrap-around and leaves front and rear unchanged. (Key point: Display)

5. After the example operations (front=2, rear=1, N=5), what does display print?
   A. 60 70 30 40 50
   B. 30 40 50 60 70
   C. 10 20 30 40 50
   D. 30 40 50
   Answer: B. From index 2 wrapping to index 1 gives 30 40 50 60 70. (Key point: Example result)

## 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×
- [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×
- [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
- [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
- [Data structure and basic operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adata-structure-basic-operations) — Data Structure and Algorithm, chapter 1
- [Data structure types and operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adata-structure-types-operations) — Data Structure and Algorithm, chapter 1
