Queue Visualizer
What is a Queue ?
A Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. Elements are added at the rear (enqueue) and removed from the front (dequeue). It operates much like a real-world queue (line) where the first person to arrive is the first to be served.
Enqueue Operation
Enqueue adds an element to the end (rear) of the queue.
Example with queue:[10, 20, 30]
The new element always goes to the end of the queue.
Example with queue:[10, 20, 30]
- Before Enqueue: [10, 20, 30]
- Enqueue(40): Add 40 to the rear
- After Enqueue: [10, 20, 30, 40]
The new element always goes to the end of the queue.
Dequeue Operation
Dequeue removes and returns the element from the front (head) of the queue.
Example with queue:[10, 20, 30, 40]
The oldest element (first one added) is always removed first.
Example with queue:[10, 20, 30, 40]
- Before Dequeue: [10, 20, 30, 40]
- Dequeue(): Remove and return 10
- After Dequeue: [20, 30, 40]
The oldest element (first one added) is always removed first.
Algorithm Steps for Enqueue
- Check if the queue is full (in case of fixed-size implementation)
- If full, return overflow error (or resize in dynamic implementation)
- Increment the rear pointer
- Add the new element at the rear position
Algorithm Steps for Dequeue
- Check if the queue is empty
- If empty, return underflow error
- Access the data at the front of the queue
- Increment the front pointer to the next element
- Return the accessed data
Time Complexity
- Enqueue Operation: O(1) - Constant time to add to the end
- Dequeue Operation: O(1) - Constant time to remove from the front
- Peek Operation: O(1) - Constant time to examine the front element
Space Complexity
The space complexity is O(n) where n is the number of elements in the queue, as it needs to store all elements.
Queues are fundamental in computer science and are used in various applications like CPU scheduling, disk scheduling, handling interrupts, breadth-first search, and any scenario where you need to maintain order of processing.
Visualize First-In-First-Out (FIFO) operations in real-time
Queue ready
Queue Visualization
Queue is empty - add some elements!
Queue Implementation (Enqueue & Dequeue Only)
// Queue Implementation in JavaScript (Linked List)
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
}
// Add element to the rear (enqueue)
enqueue(item) {
const newNode = new Node(item);
if (this.rear === null) {
this.front = this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}
// Remove element from front (dequeue)
dequeue() {
if (this.front === null) {
return "Queue Underflow";
}
const temp = this.front;
this.front = temp.next;
if (this.front === null) {
this.rear = null;
}
return temp.data;
}
}
// Usage Example
const queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log(queue.dequeue()); // 10
console.log(queue.dequeue()); // 20