DSA Queue
Process values fairly in the order they arrive using First In, First Out.
Overview
A queue is a linear data structure designed to process data in arrival order. It follows FIFO—First In, First Out—so the oldest item leaves first. Queues are widely used for printer jobs, customer support, ticket booking, CPU scheduling, messaging, and graph traversal.
Key concepts
- New elements enter at the rear
- The oldest element leaves from the front
- Enqueue adds an element
- Dequeue removes the first element
What is a Queue?
Think of customers waiting at a supermarket. The first customer to join is served first, while each new customer joins at the end. A queue works in the same way.
Front Rear
↓ ↓
[10] → [20] → [30] → [40]If an element is removed, 10 leaves first because it entered before the other values.
Why do we need a Queue?
Queues handle tasks fairly in the order they are received.
- Printer-job scheduling
- Call-center waiting systems
- Ticket-booking requests
- Food orders
- CPU process scheduling
- Message processing
Basic Queue operations
Enqueue
Enqueue adds a new value at the rear of the queue.
Before: Front → [10, 20] ← Rear
Enqueue 30
After: Front → [10, 20, 30] ← RearDequeue
Dequeue removes and returns the value at the front.
Before: Front → [10, 20, 30] ← Rear
Dequeue 10
After: Front → [20, 30] ← RearFront
The Front operation returns the first element without removing it. For [20, 30], it returns 20.
Is Empty
This operation returns true when the queue contains no values and false otherwise.
Implementing a Queue in JavaScript
A JavaScript array provides a simple learning implementation using push at the rear and shift at the front.
const queue = [];
queue.push(10);
queue.push(20);
queue.push(30);
console.log(queue); // [10, 20, 30]
queue.shift();
console.log(queue); // [20, 30]
console.log(queue[0]); // 20Using shift on a large JavaScript array is O(n) because the remaining elements are re-indexed. Production queues commonly use a head index, linked list, or circular buffer to make dequeue O(1).
Efficient Queue with a head index
const items = [10, 20, 30];
let head = 0;
const first = items[head++]; // 10
const front = items[head]; // 20
const isEmpty = head >= items.length;How a Queue changes
[]
↓ Enqueue 10
[10]
↓ Enqueue 20
[10, 20]
↓ Enqueue 30
[10, 20, 30]
↓ Dequeue
[20, 30]
↓ Enqueue 40
[20, 30, 40]Elements always enter at the rear and leave from the front.
Types of Queues
Simple Queue
A standard queue follows FIFO and processes the oldest value first.
Circular Queue
The storage wraps from the last position back to the first, allowing fixed space to be reused efficiently.
Priority Queue
Values are processed according to priority rather than arrival time. A heap commonly implements this structure.
Double-Ended Queue (Deque)
A deque allows insertion and removal at both the front and rear.
Time complexity of Queue operations
| Operation | Efficient implementation |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Front | O(1) |
| Is Empty | O(1) |
Real-life examples
At a bus stop, the person waiting longest boards first. A printer similarly processes the first submitted document before later jobs. Both systems rely on FIFO ordering.
Advantages of a Queue
- Processes data fairly
- Supports fast insertion and removal
- Is simple to understand
- Works well for task scheduling
- Handles requests sequentially
Limitations of a Queue
- Standard queues remove only from the front
- Random access is not supported
- Searching can require checking many elements
- FIFO is unsuitable when the newest or highest-priority item must go first
Where are Queues used?
- Printer management
- CPU scheduling
- Customer support
- Banking systems
- Online ticket booking
- Task scheduling
- Network packet processing
- Breadth-First Search
Tips for beginners
- Remember the FIFO rule
- Practice Enqueue and Dequeue
- Draw Front and Rear after each operation
- Compare queues with LIFO stacks
- Solve small scheduling and BFS problems
Key takeaways
- A queue follows First In, First Out
- Values enter at the rear and leave from the front
- The core operations are Enqueue, Dequeue, Front, and Is Empty
- Efficient queue operations are O(1)
- Common variants include circular queues, priority queues, and deques
- Queues support scheduling, messaging, networking, and BFS