Back to Visualizer
Queue (FIFO)
A First-In-First-Out (FIFO) data structure. The first element added is the first one to be removed. Think of a line at a ticket counter!
Enqueue: O(1)Dequeue: O(1)*Peek: O(1)
Speed:
FRONT(Dequeue)
10
front
20
30
rear
REAR(Enqueue)
← Elements flow this way ←
Default
Front
Rear
Enqueuing
Dequeuing
3
Current Size
10
Max Size
Queue Operations
- Enqueue: Add element to rear
- Dequeue: Remove element from front
- Front: View front element
- Rear: View rear element
- isEmpty: Check if queue is empty
- isFull: Check if queue is full
Real-World Uses
- • CPU task scheduling
- • Print job spooling
- • BFS traversal
- • Request handling (web servers)
- • Call center systems
Complexity
EnqueueO(1)
DequeueO(1)*
Peek (front/rear)O(1)
SpaceO(n)
* O(n) for array-based, O(1) for linked list implementation
Code
class Queue {
constructor(maxSize = 10) {
this.items = [];
this.maxSize = maxSize;
}
enqueue(value) {
if (this.isFull()) {
throw new Error("Queue Overflow");
}
this.items.push(value);
}
dequeue() {
if (this.isEmpty()) {
throw new Error("Queue Underflow");
}
return this.items.shift();
}
front() {
if (this.isEmpty()) return null;
return this.items[0];
}
rear() {
if (this.isEmpty()) return null;
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
isFull() {
return this.items.length >= this.maxSize;
}
size() {
return this.items.length;
}
}