Circular Queue Visualizer
Select an operation
3/8 items
Circular Queue Structure
Head (Dequeue)Tail (Enqueue)HighlightedPeeked
Logs
No operations yet
Notes
Circular queue is a FIFO data structure that wraps around.
Enqueue: Adds item at tail position.
Dequeue: Removes item from head position.
Peek: Views the front item without removing it.
Head and tail pointers wrap around when reaching the end.
More memory efficient than linear queue.
Fixed capacity prevents unlimited growth.
Watch for wrap-around animations!
Implementation (TypeScript)
class CircularQueue<T> {
private buffer: (T | null)[] = [];
private head: number = 0;
private tail: number = 0;
private size: number = 0;
constructor(private capacity: number) {
this.buffer = new Array(capacity).fill(null);
}
enqueue(item: T): boolean {
if (this.size >= this.capacity) return false;
this.buffer[this.tail] = item;
this.tail = (this.tail + 1) % this.capacity;
this.size++;
return true;
}
dequeue(): T | null {
if (this.size === 0) return null;
const item = this.buffer[this.head];
this.buffer[this.head] = null;
this.head = (this.head + 1) % this.capacity;
this.size--;
return item;
}
peek(): T | null {
return this.size > 0 ? this.buffer[this.head] : null;
}
isFull(): boolean { return this.size === this.capacity; }
isEmpty(): boolean { return this.size === 0; }
}About Circular Queues
A circular queue is a FIFO data structure where the end connects back to the beginning, forming a circle.
Applications:
- CPU scheduling algorithms
- Buffering data streams
- Memory management
- Traffic light systems
Advantages over Linear Queue:
- No memory waste from unused slots
- Efficient space utilization
- Fixed memory footprint
Time Complexity: All operations are O(1)
Space Complexity: O(n) where n is capacity