Understand the FIFO principle, implement a queue, analyze its operations, and explore common queue applications.
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.
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.
| 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.
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.
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.items[this.front] = undefined;
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];
}
}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.
Count the work done by the array-backed queue above. Let m be the number of enqueues since it was last empty.
| Operation | Time Complexity | Reason |
|---|---|---|
| Enqueue | O(1) amortized | Append one item. An occasional resize copies the backing array. |
| Dequeue | O(1) amortized | Read and clear one slot, then advance front. Emptying the queue releases the backing array. |
| Peek | O(1) | Read the item at the front index. |
| Is empty | O(1) | Compare the front index with the collection length. |
The queue can use O(m) slots even when few items remain. Suppose you repeatedly enqueue one value and dequeue one while another value is waiting. The queue stays short, but front keeps moving and the array keeps growing. Clearing consumed slots releases references to the old values. A circular queue reuses the slots themselves.
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.
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.
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.
| 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 |
enqueue adds, dequeue removes, peek reads, and the empty check reports whether items remain.