A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle. Unlike a simple linear queue, a circular queue connects the last position of the queue logically to the first position. This allows the program to reuse empty spaces created after deleting elements. Circular queues are commonly used in CPU scheduling, memory management, buffering, and data streaming applications.
Table of Contents
What Is a Circular Queue?
A circular queue is a special type of queue in which the last position is connected logically to the first position. It is also known as a ring buffer.
It follows the FIFO principle, meaning the first element inserted is the first element removed.
In a simple linear queue implemented using an array, deleted positions at the beginning may remain unused. A circular queue solves this problem by allowing the rear position to move back to the beginning when space is available.
Key points:
- A circular queue follows the FIFO principle.
- Insertion takes place at the rear.
- Deletion takes place at the front.
- The last position is logically connected to the first position.
- It reuses available spaces efficiently.
- It is commonly implemented using arrays.
Characteristics of a Circular Queue
A circular queue has several important characteristics that make it useful for managing data.
- FIFO Principle: The first element inserted is the first element removed.
- Circular Arrangement: The last position connects logically to the first position.
- Efficient Space Utilization: Empty positions can be reused after deletion.
- Front and Rear Pointers: It uses front and rear indexes to manage elements.
- Fixed Capacity: An array-based circular queue generally has a predefined maximum size.
- Efficient Operations: Enqueue and dequeue operations can be performed in O(1) time.
How Does a Circular Queue Work?
A circular queue works by inserting elements at the rear and removing elements from the front. When the rear reaches the last array position, it can return to the first position if that position is available.
The circular movement is managed using the modulo operator (%).
The following steps explain its working:
- Initially, the queue is empty.
- When the first element is inserted, both front and rear are set to the first position.
- Additional elements are inserted by moving the rear position forward.
- During deletion, the element at the front is removed.
- The front position moves forward to the next available element.
- When the rear reaches the final array position, it can wrap around to index 0.
- Insertion is not allowed when the circular queue is full.
The modulo operator helps keep the front and rear indexes within the valid array range.
Basic Operations of a Circular Queue
A circular queue supports several operations to insert, remove, and access elements.
1. Enqueue: Enqueue is used to insert a new element at the rear of the circular queue. Before insertion, the program checks whether the queue is full.2. Dequeue: Dequeue removes an element from the front of the circular queue. If the queue is empty, the operation cannot be performed.
3. Peek: Peek returns the front element without removing it.
4. Front: The front operation accesses the first element in the circular queue.
5. Rear: The rear operation accesses the last inserted element.
6. isEmpty(): This operation checks whether the circular queue contains any elements.
7. isFull(): This operation checks whether the circular queue has reached its maximum capacity.
Conditions of Circular Queue
A circular queue uses specific conditions to determine whether it is empty or full.
1. Empty Condition
A circular queue is empty when:
// Condition for checking an empty circular queue. front === -1
Initially, both front and rear are set to -1 to indicate that the queue contains no elements.
2. Full Condition
A circular queue is full when:
// Condition for checking a full circular queue. (rear + 1) % capacity === front
This condition checks whether the next position after rear is equal to front. If both positions match, there is no available space for another element.
Implementation of Circular Queue Using an Array
A circular queue can be implemented using a fixed-size array. Two variables, front and rear, are used to manage the positions of elements.
- front stores the index of the first element.
- rear stores the index of the last element.
- capacity defines the maximum number of elements.
- The modulo operator helps move indexes back to the beginning of the array.
JavaScript Example:
// This program demonstrates a circular queue using an array.
let capacity = 5;
let queue = new Array(capacity);
let front = -1;
let rear = -1;
function enqueue(element) {
if ((rear + 1) % capacity === front) {
console.log("Queue Overflow");
return;
}
if (front === -1) {
front = 0;
}
rear = (rear + 1) % capacity;
queue[rear] = element;
console.log(element + " inserted");
}
function dequeue() {
if (front === -1) {
console.log("Queue Underflow");
return;
}
let removed = queue[front];
if (front === rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % capacity;
}
console.log("Removed:", removed);
}
function display() {
let elements = [];
if (front === -1) {
console.log("Queue is empty");
return;
}
let i = front;
while (true) {
elements.push(queue[i]);
if (i === rear) {
break;
}
i = (i + 1) % capacity;
}
console.log("Queue:", elements);
}
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
enqueue(40);
enqueue(50);
display();
Output:
10 inserted
20 inserted
30 inserted
Removed: 10
40 inserted
50 inserted
Queue: [20, 30, 40, 50]
In this example, a circular queue with a capacity of five is created. Elements are inserted using the enqueue() function and removed using the dequeue() function. The display() function prints the elements in their correct FIFO order.
Implementation of Circular Queue Using a Class in JavaScript
A circular queue can also be implemented using a JavaScript class. This approach keeps the queue data and its operations together.
JavaScript Example:
// This program implements a circular queue using a JavaScript class.
class CircularQueue {
constructor(capacity) {
this.capacity = capacity;
this.queue = new Array(capacity);
this.front = -1;
this.rear = -1;
}
isEmpty() {
return this.front === -1;
}
isFull() {
return (this.rear + 1) % this.capacity === this.front;
}
enqueue(element) {
if (this.isFull()) {
console.log("Queue is full");
return;
}
if (this.isEmpty()) {
this.front = 0;
}
this.rear = (this.rear + 1) % this.capacity;
this.queue[this.rear] = element;
}
dequeue() {
if (this.isEmpty()) {
console.log("Queue is empty");
return;
}
let removed = this.queue[this.front];
if (this.front === this.rear) {
this.front = -1;
this.rear = -1;
} else {
this.front = (this.front + 1) % this.capacity;
}
return removed;
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.queue[this.front];
}
}
let queue = new CircularQueue(3);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log("Front Element:", queue.peek());
console.log("Removed:", queue.dequeue());
queue.enqueue(40);
console.log("Removed:", queue.dequeue());
Output:
Front Element: 10
Removed: 10
Removed: 20
In this example, the CircularQueue class contains methods for checking the queue status, inserting elements, removing elements, and accessing the front element. The modulo operator allows the queue to reuse available positions.
Algorithm of Circular Queue
The following algorithms explain the basic enqueue and dequeue operations in a circular queue.
Enqueue Algorithm:
- Start.
- Check whether (rear + 1) % MAX == front.
- If true, display "Queue Overflow".
- Otherwise, check whether front == -1.
- If true, set front = 0.
- Update rear = (rear + 1) % MAX.
- Insert the new element at queue[rear].
- Stop.
Dequeue Algorithm:
- Start.
- Check whether front == -1.
- If true, display "Queue Underflow".
- Otherwise, store the element at queue[front].
- If front == rear, set both front and rear to -1.
- Otherwise, update front = (front + 1) % MAX.
- Return the removed element.
- Stop.
These algorithms ensure that elements are inserted and removed correctly while maintaining the circular structure.
Time Complexity of Circular Queue
Time complexity describes how the execution time of an operation changes as the number of elements increases.
The following table shows the time complexity of common circular queue operations.
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
| Front | O(1) |
| Rear | O(1) |
| isEmpty() | O(1) |
| isFull() | O(1) |
| Display | O(n) |
Enqueue and dequeue take O(1) time because they update indexes without shifting the remaining elements.
Advantages of Circular Queue
A circular queue provides several benefits when managing data.
- Better Space Utilization: It reuses empty positions created after deletion.
- Efficient Operations: Enqueue and dequeue can be performed in O(1) time.
- Avoids False Overflow: It prevents the unused spaces at the beginning of a linear array queue from remaining inaccessible.
- Suitable for Buffers: It is useful for temporarily storing data during processing.
- Organized Processing: It maintains the FIFO order of elements.
Disadvantages of Circular Queue
Although a circular queue is efficient, it also has some limitations.
- Complex Implementation: It requires careful management of front and rear indexes.
- Fixed Capacity: An array-based circular queue has a predefined maximum size.
- Full and Empty Conditions: The program must correctly distinguish between a full queue and an empty queue.
- Difficult Debugging: Incorrect index updates can cause elements to be skipped or overwritten.
- Limited Flexibility: A fixed-size implementation cannot automatically grow when more elements are required.
Applications of Circular Queue
Circular queues are used in several real-world and computer science applications.
- CPU Scheduling: Operating systems can use circular queues in Round Robin scheduling to give processes turns to execute.
- Memory Management: Circular queues help manage reusable memory buffers.
- Keyboard Buffers: They temporarily store keystrokes before they are processed.
- Data Streaming: They manage incoming and outgoing data in streaming systems.
- Network Communication: They store data packets waiting to be processed or transmitted.
- Traffic Management: They can help manage cyclic processing of requests and resources.
Difference Between Simple Queue and Circular Queue
A simple queue and a circular queue both follow the FIFO principle, but they differ in how they manage available space.
| Simple Queue | Circular Queue |
|---|---|
| Uses a linear arrangement. | Uses a logically circular arrangement. |
| Rear moves forward toward the last position. | Rear can wrap around to the first position. |
| Deleted positions may remain unused in a basic array implementation. | Deleted positions can be reused. |
| May experience false overflow in a fixed-size array implementation. | Avoids false overflow by reusing available positions. |
| Space utilization may be inefficient. | Space utilization is more efficient. |
| Implementation is comparatively simple. | Requires circular index management. |
Common Mistakes to Avoid
- Incorrect Modulo Operation: Use the modulo operator to keep front and rear indexes within the valid array range.
- Ignoring the Full Condition: Check whether the next rear position is equal to front before inserting an element.
- Ignoring the Empty Condition: Check whether front is -1 before performing a dequeue operation.
- Forgetting to Reset Pointers: When the last element is removed, reset both front and rear to -1.
- Confusing Circular and Linear Queues: Remember that a circular queue allows indexes to wrap around and reuse available positions.
Frequently Asked Questions
1. What is a circular queue?
A circular queue is a queue in which the last position is logically connected to the first position to reuse available spaces.
2. Which principle does a circular queue follow?
A circular queue follows the FIFO (First In, First Out) principle.
3. What is the main advantage of a circular queue?
Its main advantage is efficient space utilization because it reuses positions that become empty after deletion.
4. How is a circular queue different from a simple queue?
A simple queue uses a linear arrangement, while a circular queue allows the rear and front indexes to wrap around to the beginning of the array.
5. What is the time complexity of enqueue and dequeue in a circular queue?
Both enqueue and dequeue take O(1) time because they update the front and rear indexes without shifting elements.
0 Comments