Queue Operations
Enqueue and dequeue a live FIFO queue, watch items wait their turn in arrival order.
Enqueue and dequeue a live FIFO queue, watch items wait their turn in arrival order.
op 1 / 1
What's happening
A queue serves in arrival order: enqueue joins at the REAR, dequeue leaves from the FRONT. First in, first out.
FIFO is the structure of fairness, schedulers, buffers, and BFS all promise arrival-order service. A naive array + shift() pays O(n) per dequeue; ring buffers and linked lists keep it O(1).
Understand the algorithm
What it is
A queue is a FIFO (first-in, first-out) collection: enqueue joins at the rear, dequeue leaves from the front, exactly like a fair waiting line.
Why it matters
FIFO is the structure of fairness and buffering. Every task scheduler, print queue, message broker, and breadth-first search runs on it. The subtle engineering lesson is that a naive array queue is O(n) per dequeue, the ring buffer fix is a classic.
How it works
- 1enqueue(value): append at the rear, O(1).
- 2dequeue(): remove from the front, O(1) with a proper structure.
- 3peek(): read the front; it is the next to be served.
- 4Elements wait strictly in arrival order, no jumping the line.
Real-world uses
- CPU and task scheduling; job queues
- Breadth-first search's frontier (the ring order IS the queue)
- Message queues between services (Kafka, RabbitMQ concepts)
- Print spoolers, customer waitlists, rate limiters
Strengths
- O(1) enqueue and dequeue with a linked list or ring buffer
- Strict fairness, arrival order is service order
- Natural fit for producer/consumer pipelines
Limitations
- No random access or priority, urgent items still wait their turn (that is a priority queue)
- Array-based queues shift on dequeue unless implemented as a ring buffer
- Front and rear must both be tracked, more bookkeeping than a stack