A Circular Queue is an advanced version of a linear queue where the last position is connected back to the first position to form a circle. This structure helps in better memory utilization because empty spaces created after deletion can be reused efficiently. Circular queues are widely used in computer science applications such as CPU scheduling, memory management, buffering, and real-time systems.
In a standard queue, elements are inserted from the rear and removed from the front following the FIFO (First In First Out) principle. However, in a linear queue, unused spaces cannot be reused once the rear reaches the end of the array. A circular queue solves this problem by allowing the rear pointer to move back to the beginning of the queue.
What is a Circular Queue?
A Circular Queue is a linear data structure that follows FIFO order and connects the last position to the first position. The queue operates in a circular manner, which means the rear pointer wraps around when it reaches the end of the array.
Key Features of Circular Queue:
- Efficient memory utilization.
- Follows FIFO order.
- Rear pointer wraps around to the beginning.
- Suitable for fixed-size memory allocation.
- Prevents memory wastage.
Real-World Applications of Circular Queue?
Circular queues are commonly used in many real-world applications.
| Application | Description |
|---|---|
| CPU Scheduling | Used in Round Robin Scheduling algorithms. |
| Keyboard Buffer | Stores keyboard input efficiently. |
| Traffic Systems | Manages traffic light sequences. |
| Streaming Services | Handles continuous data streams. |
| Printer Queue | Maintains print jobs in order. |
Basic Operations of Circular Queue?
A Circular Queue supports several important operations.
1. Enqueue Operation.
The enqueue operation inserts an element into the queue from the rear end.
Steps of Enqueue.
- Check if the queue is full.
- If the queue is empty, initialize front and rear.
- Otherwise, move rear using circular increment.
- Insert the element.
2. Dequeue Operation.
The dequeue operation removes an element from the front of the queue.
Steps of Dequeue.
- Check if the queue is empty.
- Remove the front element.
- If only one element exists, reset front and rear.
- Otherwise, move front using circular increment.
3. Display Operation.
The display operation shows all queue elements.
4. Peek Operation.
The peek operation returns the front element without removing it.
Conditions in Circular Queue?
Understanding full and empty conditions is important while implementing a circular queue.
| Condition | Formula |
|---|---|
| Queue Empty | front == -1 |
| Queue Full | (rear + 1) % size == front |
Working of Circular Queue?
Suppose the queue size is 5.
- Insert elements into the queue.
- When the rear reaches the last index, it wraps to the first index.
- Deleted spaces are reused for new insertions.
This circular movement improves memory efficiency and prevents unnecessary overflow.
Circular Queue Implementation in Python?
Below is a complete implementation of Circular Queue using Python.
class CircularQueue:
def __init__(self, size):
self.size = size
self.queue = [None] * size
self.front = -1
self.rear = -1
# Enqueue operation
def enqueue(self, data):
if ((self.rear + 1) % self.size == self.front):
print("Queue is Full")
elif (self.front == -1):
self.front = 0
self.rear = 0
self.queue[self.rear] = data
else:
self.rear = (self.rear + 1) % self.size
self.queue[self.rear] = data
# Dequeue operation
def dequeue(self):
if (self.front == -1):
print("Queue is Empty")
elif (self.front == self.rear):
temp = self.queue[self.front]
self.front = -1
self.rear = -1
return temp
else:
temp = self.queue[self.front]
self.front = (self.front + 1) % self.size
return temp
# Display operation
def display(self):
if(self.front == -1):
print("Queue is Empty")
elif (self.rear >= self.front):
for i in range(self.front, self.rear + 1):
print(self.queue[i], end=" ")
print()
else:
for i in range(self.front, self.size):
print(self.queue[i], end=" ")
for i in range(0, self.rear + 1):
print(self.queue[i], end=" ")
print()
# Driver Code
cq = CircularQueue(5)
cq.enqueue(10)
cq.enqueue(20)
cq.enqueue(30)
cq.enqueue(40)
print("Queue Elements:")
cq.display()
print("Deleted Element:", cq.dequeue())
cq.enqueue(50)
cq.enqueue(60)
print("Updated Queue:")
cq.display()
Output of the Program:
Queue Elements:
10 20 30 40
Deleted Element: 10
Updated Queue:
20 30 40 50 6
Advantages of Circular Queue?
Circular Queue provides several advantages over a linear queue.
| Advantage | Explanation |
|---|---|
| Better Memory Usage | Reuses empty spaces efficiently. |
| Faster Operations | Enqueue and dequeue operations are efficient. |
| Avoids Memory Wastage | No unused space remains after deletion. |
| Suitable for Buffers | Perfect for continuous data flow. |
| Improves Performance | Useful in multitasking systems. |
Disadvantages of Circular Queue?
Although circular queues are useful, they also have some limitations.
| Disadvantage | Explanation |
|---|---|
| Fixed Size | Queue size must be predefined. |
| Complex Logic | Slightly harder to implement than linear queues. |
| Overflow Condition | Difficult to differentiate full and empty conditions sometimes |
Difference Between Linear Queue and Circular Queue?
| Feature | Linear Queue | Circular Queue |
|---|---|---|
| Memory Utilization | Inefficient | Efficient |
| Rear Movement | One Direction | Circular Movement |
| Space Reusability | Not Possible | Possible |
| Performance | Lower | Better |
| Overflow Chances | Higher | Lower |
Time Complexity of Circular Queue Operations:
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
| Display | O(n) |
Common Interview Questions on Circular Queue?
1. Why is Circular Queue better than Linear Queue?
Circular Queue utilizes memory efficiently and avoids memory wastage.
2. What is the condition for a full circular queue?
A circular queue is full when:
(rear + 1) % size == front
3. Where are Circular Queues used?
Circular queues are used in CPU scheduling, buffering systems, and streaming applications.
Final Thoughts.
Circular Queue is one of the most important data structures used for efficient memory management and process scheduling. It solves the memory wastage problem found in linear queues by connecting the rear end to the front end. Understanding its operations and implementation is essential for coding interviews, software development, and system design.
Python makes Circular Queue implementation simple and readable, making it a great language for learning data structures. By mastering circular queues, developers can build optimized applications that handle continuous data efficiently.
FAQs.
1. What is a Circular Queue in data structure?
A Circular Queue is a queue where the last position connects back to the first position, forming a circular structure.
2. What is the main advantage of Circular Queue?
The main advantage is efficient memory utilization because deleted spaces can be reused.
3. Which principle does Circular Queue follow?
Circular Queue follows the FIFO (First In First Out) principle.
4. What is the time complexity of enqueue and dequeue operations?
Both enqueue and dequeue operations take O(1) time complexity.
5. Is Circular Queue used in operating systems?
Yes, circular queues are commonly used in CPU scheduling and buffering systems.





