Circular Queue
A Circular Queue is a linear data structure that follows the FIFO (First In, First Out) principle, but connects the last position of the queue back to the first position. Unlike a simple array-based queue, a Circular Queue can reuse empty positions created after deletion. It is useful in memory management, CPU scheduling, buffering, and many other applications.
1. What Is a Circular Queue?
A Circular Queue is a special type of queue in which the last array position is connected to the first position. It behaves like a circle rather than a straight line.
Like a normal queue, it follows FIFO:
- Insertion takes place at the rear.
- Deletion takes place at the front.
The important difference is that when the rear reaches the end of the array, it can move back to the beginning if an empty position is available.
Simple Illustration
+-----+ +-----+
| 0 |-----| 1 |
+-----+ +-----+
| |
| |
+-----+ +-----+
| 4 |-----| 2 |
+-----+ +-----+
| |
+----+------+
|
+-----+
| 3 |
+-----+
The last position connects back to the first position. This circular arrangement allows the queue to reuse available array locations.
2. Why Do We Need a Circular Queue?
To understand the need for a Circular Queue, first consider a normal array-based queue.
Problem with a Simple Queue
Suppose we have an array of size 5:
Index: 0 1 2 3 4
+---+---+---+---+---+
Queue: |10 |20 |30 |40 |50 |
+---+---+---+---+---+
Now remove two elements from the front:
Index: 0 1 2 3 4
+---+---+---+---+---+
Queue: | | |30 |40 |50 |
+---+---+---+---+---+
Rear
Positions 0 and 1 are now empty. However, in a simple linear queue, if rear is already at the last index, we cannot insert another element without shifting existing elements.
This leads to wasted space, also called false overflow when the implementation treats the queue as full despite free positions at the beginning.
How Circular Queue Solves This Problem
A Circular Queue allows rear to move back to index 0 when space is available.
Before Reuse:
Index: 0 1 2 3 4
+---+---+---+---+---+
Queue: | | |30 |40 |50 |
+---+---+---+---+---+
After Inserting 60 and 70:
Index: 0 1 2 3 4
+---+---+---+---+---+
Queue: |60 |70 |30 |40 |50 |
+---+---+---+---+---+
The available positions at the beginning are reused without shifting the remaining elements.
3. Important Variables in a Circular Queue
An array-based Circular Queue generally uses the following variables:
- arr: Stores the queue elements.
- front: Stores the index of the first element.
- rear: Stores the index of the last element.
- size: Stores the maximum capacity of the queue.
- count: Stores the current number of elements, when using a count-based implementation.
Initially, both front and rear can be set to -1 to indicate that the queue is empty.
4. Important Conditions
There are two important conditions in a Circular Queue.
Queue Is Empty
front == -1
This condition indicates that there are no elements in the queue. It is used in the implementation given below.
Queue Is Full
(rear + 1) % size == front
This condition checks whether the next circular position after rear is equal to front. If it is, the queue is full.
5. Basic Operations of a Circular Queue
1. Enqueue
Enqueue inserts a new element at the rear of the queue.
- Check whether the queue is full.
- If it is full, insertion is not possible.
- If the queue is empty, set front and rear to 0.
- Otherwise, update rear using the modulo operation.
- Insert the new element at the rear position.
rear = (rear + 1) % size;
arr[rear] = value;
2. Dequeue
Dequeue removes the front element from the queue.
- Check whether the queue is empty.
- If it is empty, deletion is not possible.
- Store or return the element at front.
- If front and rear are equal, the queue had only one element. Reset both to -1.
- Otherwise, move front to the next circular position.
front = (front + 1) % size;
3. Get Front
Returns the element at the front without removing it.
4. Get Rear
Returns the last element without removing it.
5. isEmpty
Checks whether the queue contains no elements.
6. isFull
Checks whether the queue has reached its maximum capacity.
6. Circular Queue Implementation Using an Array in C++
Let us implement a Circular Queue using a fixed-size array. We will use front and rear variables to manage insertion and deletion.
Complete C++ Program
#include <bits/stdc++.h>
using namespace std;
class CircularQueue {
private:
int* arr;
int front;
int rear;
int size;
int count;
public:
CircularQueue(int capacity) {
size = capacity;
arr = new int[size];
front = 0;
rear = -1;
count = 0;
}
~CircularQueue() {
delete[] arr;
}
bool isEmpty() {
return count == 0;
}
bool isFull() {
return count == size;
}
void enqueue(int value) {
if (isFull()) {
cout << "Queue is Full" << endl;
return;
}
rear = (rear + 1) % size;
arr[rear] = value;
count++;
cout << value << " inserted" << endl;
}
int dequeue() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return -1;
}
int value = arr[front];
front = (front + 1) % size;
count--;
if (count == 0) {
front = 0;
rear = -1;
}
return value;
}
int getFront() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return -1;
}
return arr[front];
}
int getRear() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return -1;
}
return arr[rear];
}
int getSize() {
return count;
}
void display() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
cout << "Queue: ";
for (int i = 0; i < count; i++) {
int index = (front + i) % size;
cout << arr[index] << " ";
}
cout << endl;
}
};
int main() {
CircularQueue q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.display();
cout << "Deleted: " << q.dequeue() << endl;
cout << "Deleted: " << q.dequeue() << endl;
q.enqueue(50);
q.enqueue(60);
q.display();
cout << "Front: " << q.getFront() << endl;
cout << "Rear: " << q.getRear() << endl;
return 0;
}
Output
10 inserted
20 inserted
30 inserted
40 inserted
Queue: 10 20 30 40
Deleted: 10
Deleted: 20
50 inserted
60 inserted
Queue: 30 40 50 60
Front: 30
Rear: 60
7. Understanding the C++ Code
Step 1: Constructor
CircularQueue(int capacity) {
size = capacity;
arr = new int[size];
front = 0;
rear = -1;
count = 0;
}
The constructor creates the array and initializes the queue. The count variable starts at zero because no elements are present.
Step 2: Checking Whether the Queue Is Full
bool isFull() {
return count == size;
}
When the number of stored elements becomes equal to the array capacity, the queue is full.
Step 3: Enqueue Operation
rear = (rear + 1) % size;
arr[rear] = value;
count++;
The modulo operation moves rear to the next valid circular position. The element is inserted there and count is increased.
Step 4: Dequeue Operation
int value = arr[front];
front = (front + 1) % size;
count--;
The front element is saved, then front moves to the next circular position. The element count is reduced.
Step 5: Resetting an Empty Queue
if (count == 0) {
front = 0;
rear = -1;
}
If the last element is removed, the queue returns to its initial empty state.
8. Dry Run of Circular Queue
Consider a Circular Queue with capacity 5.
Queue Size = 5
Perform these operations:
enqueue(10)
enqueue(20)
enqueue(30)
enqueue(40)
dequeue()
dequeue()
enqueue(50)
enqueue(60)
| Operation | Front | Rear | Queue Elements |
|---|---|---|---|
| Initial | 0 | -1 | Empty |
| enqueue(10) | 0 | 0 | 10 |
| enqueue(20) | 0 | 1 | 10, 20 |
| enqueue(30) | 0 | 2 | 10, 20, 30 |
| enqueue(40) | 0 | 3 | 10, 20, 30, 40 |
| dequeue() | 1 | 3 | 20, 30, 40 |
| dequeue() | 2 | 3 | 30, 40 |
| enqueue(50) | 2 | 4 | 30, 40, 50 |
| enqueue(60) | 2 | 0 | 30, 40, 50, 60 |
Notice that after inserting 60, rear moves from index 4 back to index 0.
9. Circular Queue Full and Empty Conditions
There are two common ways to manage a Circular Queue:
Method 1: Using a Count Variable
Empty: count == 0
Full: count == size
This approach stores the current number of elements and makes it easy to distinguish between a full queue and an empty queue.
Method 2: Using Front and Rear Only
Another common approach uses one unused array position to distinguish between full and empty conditions.
Empty: front == -1
Full: (rear + 1) % size == front
In this method, the full condition means the next position after rear is already occupied by front.
10. Circular Queue vs Simple Queue
| Feature | Simple Queue | Circular Queue |
|---|---|---|
| Arrangement | Linear | Circular |
| Space Reuse | May waste freed positions in a simple array implementation | Reuses freed positions |
| Pointer Movement | Moves forward | Moves circularly |
| Memory Efficiency | Can be less efficient with a fixed array | Uses available array positions efficiently |
| Implementation | Simpler | Requires circular index handling |
11. Time and Space Complexity
A Circular Queue implemented using an array provides constant-time insertion and deletion operations.
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Get Front | O(1) |
| Get Rear | O(1) |
| isEmpty | O(1) |
| isFull | O(1) |
| Display | O(n) |
Space Complexity: O(n), where n is the fixed capacity of the queue.
12. Applications of Circular Queue
- CPU Scheduling: Round Robin scheduling can use a circular queue to repeatedly process tasks.
- Memory Management: Circular buffers reuse memory locations efficiently.
- Keyboard Buffers: Input data can be stored temporarily in a circular buffer.
- Network Buffers: Network devices can use circular buffers to manage incoming and outgoing data.
- Streaming Systems: Data streams can be handled using fixed-size circular buffers.
- Traffic Systems: Cyclic scheduling and repeated processing can use circular queue concepts.
13. Common Mistakes
- Forgetting the modulo operator: Use modulo to move indices back to the beginning.
- Incorrect full condition: A full queue must be detected before insertion.
- Incorrect empty condition: Never remove an element from an empty queue.
- Not resetting pointers: When the last element is removed, restore the initial empty state.
- Confusing front and rear: Insertion occurs at rear, while deletion occurs at front.
- Not handling capacity correctly: A queue of capacity zero cannot store elements.
- Ignoring wraparound: The rear or front may move from the last index to index zero.
14. Interview Questions
- What is a Circular Queue?
- Why do we need a Circular Queue?
- What is the difference between a Simple Queue and a Circular Queue?
- How does the modulo operator help in a Circular Queue?
- What is the full condition of a Circular Queue?
- What is the empty condition of a Circular Queue?
- How can a Circular Queue be implemented using an array?
- What happens when the rear reaches the last index?
- What is the time complexity of enqueue and dequeue?
- What are the applications of Circular Queues?
- How is a Circular Queue useful in Round Robin scheduling?
- What is a circular buffer?
15. Practice Problems
- Implement a Circular Queue using an array.
- Implement a Circular Queue using a linked list.
- Design a fixed-size circular queue with enqueue, dequeue, front, rear, and isEmpty operations.
- Handle full and empty conditions correctly in a Circular Queue.
- Implement a circular buffer for a stream of incoming data.
- Use a Circular Queue to simulate Round Robin CPU scheduling.
16. Summary
- A Circular Queue is a special type of queue that connects the last position to the first.
- It follows the FIFO principle.
- Insertion happens at the rear, and deletion happens at the front.
- The modulo operator is used for circular movement.
- It reuses empty positions created by deletion.
- Enqueue and dequeue operations take O(1) time.
- It is useful in CPU scheduling, buffering, and networking.