A linear data structure that follows the First-In, First-Out (FIFO) principle. Elements are added (enqueued) at the rear and removed (dequeued) from the front, like a line of people.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
{
"status": "Initializing queue"
}Overview
A queue is a First-In, First-Out (FIFO) collection: items are added at the rear and removed from the front, like a line at a ticket counter. The first item to arrive is always the first to be served.
Queues model anything that should be handled in arrival order: print jobs, network packets, requests to a web server, or messages between services. They are also the engine of breadth-first search, where the queue guarantees nodes are explored level by level.
The default commands enqueue 10 and 20, dequeue (removing 10, the oldest), enqueue 30 and 40, then dequeue again (removing 20). The final queue is [30, 40], with 30 at the front.
Step by step
enqueue(x): add x at the rear.dequeue(): remove and return the front element; handle the empty case.peek(): look at the front element without removing it.isEmpty()/size(): check how many elements are waiting.
Complexity
| Case | Cost | Why |
|---|---|---|
| Enqueue | O(1) | Append at the rear. |
| Dequeue | O(1) | With a linked list, circular buffer or head index; O(n) with Array.shift(). |
| Peek | O(1) | Read the front element. |
| Space | O(n) | One slot per element. |
When to use it
- Breadth-first search and shortest paths in unweighted graphs.
- Task scheduling and job processing in arrival order.
- Buffers between a producer and a consumer running at different speeds.
Common mistakes
- Implementing dequeue with
Array.shift()in JavaScript moves every element, making it O(n). - A naive array queue that only moves the front index forward wastes the space behind it — the circular queue fixes this.
- Forgetting the empty-queue check on dequeue.
A dry run is the best way to check your understanding of Queue: follow each row, predict the next one, and compare. The run below takes 14 steps in total. The Line column refers to the line numbers in the code listing above.
- Input
- enqueue 10,enqueue 20,dequeue,enqueue 30,enqueue 40,dequeue
- Result
- status = Operations complete, final_queue = [30, 40]
| Step | Line | Array | What happens |
|---|---|---|---|
| 1 | 2 | [] | status = Initializing queue |
| 2 | 4 | [] | action = enqueue(10) |
| 3 | 5 | [10] | status = Enqueued 10, front = 10, rear = 10 |
| 4 | 4 | [10] | action = enqueue(20) |
| 5 | 5 | [10, 20] | status = Enqueued 20, front = 10, rear = 20 |
| 6 | 7 | [10, 20] | action = dequeue() |
| 7 | 11 | [20] | status = Dequeued 10, front = 20, rear = 20 |
| 8 | 4 | [20] | action = enqueue(30) |
| 9 | 5 | [20, 30] | status = Enqueued 30, front = 20, rear = 30 |
| 10 | 4 | [20, 30] | action = enqueue(40) |
| 11 | 5 | [20, 30, 40] | status = Enqueued 40, front = 20, rear = 40 |
| 12 | 7 | [20, 30, 40] | action = dequeue() |
| 13 | 11 | [30, 40] | status = Dequeued 20, front = 30, rear = 40 |
| 14 | 18 | [30, 40] | status = Operations complete, final_queue = [30, 40] |
What is a Queue?
What are the primary operations of a Queue?
- Enqueue: Adds an element to the rear (end) of the queue.
- Dequeue: Removes an element from the front of the queue.
- Front (or Peek): Returns the first element without removing it.
- isEmpty: Checks if the queue is empty.
What is the time complexity of queue operations?
Where are queues used in computing?
- Operating Systems: For managing processes in CPU scheduling.
- Networking: To handle data packets in order.
- Printers: To manage print jobs in the order they were received.
- Breadth-First Search (BFS): The algorithm uses a queue to explore graph nodes level by level.