circularQueue.definition
circularQueue.descriptionPart1
circularQueue.descriptionPart2
circularQueue.keyPointsTitle
- circularQueue.keyPoint1
- circularQueue.keyPoint2
- circularQueue.keyPoint3
- circularQueue.keyPoint4
circularQueue.exampleTitle
100circularQueue.frontLabel
201
302circularQueue.rearLabel
—3
—4
—5
—6
—7
↻ Wraps around (rear + 1) % capacity
circularQueue.syntaxTitle
// Java - Circular Queue
class CircularQueue {
private int[] arr;
private int front, rear, size, capacity;
public CircularQueue(int cap) {
capacity = cap;
arr = new int[capacity];
front = rear = size = 0;
}
void enqueue(int val) {
if (size == capacity) grow();
arr[rear] = val;
rear = (rear + 1) % capacity;
size++;
}
int dequeue() {
int val = arr[front];
front = (front + 1) % capacity;
size--;
return val;
}
}# Python - Circular Queue
class CircularQueue:
def __init__(self, cap=4):
self.arr = [None] * cap
self.front = self.rear = self.size = 0
self.capacity = cap
def enqueue(self, val):
if self.size == self.capacity:
self._grow()
self.arr[self.rear] = val
self.rear = (self.rear + 1) % self.capacity
self.size += 1
def dequeue(self):
val = self.arr[self.front]
self.front = (self.front + 1) % self.capacity
self.size -= 1
return val// C++ - Circular Queue
class CircularQueue {
int* arr;
int front, rear, sz, cap;
public:
CircularQueue(int c = 4) : cap(c), front(0), rear(0), sz(0) {
arr = new int[cap];
}
void enqueue(int val) {
if (sz == cap) grow();
arr[rear] = val;
rear = (rear + 1) % cap;
sz++;
}
int dequeue() {
int val = arr[front];
front = (front + 1) % cap;
sz--;
return val;
}
};// JavaScript - Circular Queue
class CircularQueue {
constructor(cap = 4) {
this.arr = new Array(cap).fill(null);
this.front = this.rear = this.size = 0;
this.capacity = cap;
}
enqueue(val) {
if (this.size === this.capacity) this.grow();
this.arr[this.rear] = val;
this.rear = (this.rear + 1) % this.capacity;
this.size++;
}
dequeue() {
const val = this.arr[this.front];
this.front = (this.front + 1) % this.capacity;
this.size--;
return val;
}
}Interactive Circular Queue Visualization
—0
—1
—2
—3
—4
—5
—6
—7
↻ Index wraps: (i + 1) % 8
Front:—
Rear:—
Size:0
Capacity:8
Status:Empty
Empty
Front
Rear
Filled
Searching
Found
Note: This circular queue uses modular arithmetic for wrap-around. When full, capacity doubles and elements are reordered. Enqueue/Dequeue are O(1) amortized.
Time & Space Complexity
A circular queue uses modular arithmetic to wrap indices, enabling O(1) enqueue and dequeue without shifting elements. With dynamic resizing, enqueue is amortized O(1).
| Operation | Time | Why? |
|---|---|---|
| Enqueue | O(1)* | Add at rear index, advance rear = (rear + 1) % capacity |
| Dequeue | O(1) | Remove at front index, advance front = (front + 1) % capacity |
| Peek / Front | O(1) | Access element at front index |
| Search | O(n) | Scan from front to rear in logical order |
| isEmpty | O(1) | Check if size == 0 |
| isFull | O(1) | Check if size == capacity (fixed-capacity variant) |
| Size | O(1) | Return stored size counter |
| Space | O(n) | Array storage for n elements |
vs Linear Queue
- Advantage: Dequeue is O(1) - no element shifting required
- Advantage: Better memory utilization with wrap-around
- Trade-off: Slightly more complex index management
- Note: * With resizing, enqueue is amortized O(1)
circularQueue.continueLearning
enhanced.author.title
enhanced.author.writtenBy:enhanced.author.teamName
enhanced.author.reviewedBy:enhanced.author.reviewerName
enhanced.author.lastUpdated:January 29, 2026