Queue
Understand the FIFO principle, implement a queue, analyze its operations, and explore common queue applications.
Queue
A queue is a linear data structure that follows FIFO: First In, First Out. The first item added is the first item removed.
A line at a grocery store works the same way. New customers join at the back, or rear, while the customer who has waited the longest leaves from the front after being served.
Two Ends
A queue accepts new items at the rear and removes existing items from the front. Keeping these roles separate preserves FIFO order.
How FIFO Works
Start with an empty queue and follow three operations:
| Operation | Queue State | What Happened |
|---|---|---|
enqueue(10) | [10] | 10 enters at the rear and is also at the front. |
enqueue(20) | [10, 20] | 20 joins behind 10. |
dequeue() | [20] | 10, the first item added, is removed first. |
The insertion order is 10, then 20. The removal order stays the same: 10, then 20.
Core Operations
| Operation | Purpose |
|---|---|
| Enqueue | Add an item at the rear. |
| Dequeue | Remove and return the item at the front. |
| Peek | Read the front item without removing it. |
| Is Empty | Check whether the queue contains any items. |
Enqueue and dequeue define FIFO behavior. Peek and empty checks let code inspect the queue safely.
Implementing a Queue
The queue stores its items in an array or list and uses a front index to track the next item to remove. Enqueue adds at the end. Dequeue reads the current front item and moves the index forward.
Queue Class
class Queue {
constructor() {
this.items = [];
this.front = 0;
}
isEmpty() {
return this.front >= this.items.length;
}
enqueue(item) {
this.items.push(item);
}
dequeue() {
if (this.isEmpty()) {
throw new Error("Queue is empty");
}
const item = this.items[this.front];
this.front++;
if (this.isEmpty()) {
this.items = [];
this.front = 0;
}
return item;
}
peek() {
if (this.isEmpty()) {
throw new Error("Queue is empty");
}
return this.items[this.front];
}
}Using the Queue
const queue = new Queue();
queue.enqueue("first");
queue.enqueue("second");
queue.enqueue("third");
console.log(queue.peek()); // "first"
console.log(queue.dequeue()); // "first"
console.log(queue.dequeue()); // "second"
console.log(queue.isEmpty()); // falseThe first value enqueued is also the first value dequeued, so the implementation preserves FIFO order.
Empty Queue Operations
The implementation raises an error when dequeue or peek is called on an empty queue. Use the empty-check method first when an empty queue is possible.
Performance and Big O
Big O notation describes how an operation's running time changes as the input grows.
- Constant time — : The work does not increase with the number of items.
- Linear time — : The work grows with the number of items.
| Operation | Time Complexity | Reason |
|---|---|---|
| Enqueue | Add at the rear. | |
| Dequeue | Read the front item and advance the front index. | |
| Peek | Read the item at the front index. | |
| Is empty | Compare the front index with the collection length. |
The queue does not need to scan its existing items for any core operation.
Applications of Queues
Print Spooling
When several devices send jobs to one printer, the queue preserves the order in which those jobs arrived. The printer handles one job at a time, preventing jobs from overlapping.
Background Processing
Applications can place work in a queue so it can be processed outside the main application flow. This keeps the main application responsive while tasks wait for a worker.
For example, confirmation emails can be queued as users trigger them. A worker then processes the emails sequentially instead of making each user wait for sending to finish.
Task Scheduling and Shared Resources
Queues help manage work that must be handled fairly and in arrival order. This makes them useful when multiple tasks are waiting for the same resource.
Stack vs. Queue
| Feature | Stack | Queue |
|---|---|---|
| Ordering | Last In, First Out | First In, First Out |
| Add operation | Push at the top | Enqueue at the rear |
| Remove operation | Pop from the top | Dequeue from the front |
| Everyday analogy | Stack of plates | Waiting line |
Key Takeaways
- A queue follows First In, First Out order.
- New items enter at the rear, and existing items leave from the front.
enqueueadds,dequeueremoves,peekreads, and the empty check reports whether items remain.- A well-implemented queue performs its core operations in time.
- Print spooling, background jobs, and task scheduling use queues to preserve arrival order.