The FIFO Model
First In, First Out
A queue is a data structure that processes items in the order they arrive. The first item you add is the first item you get back. This is called FIFO: First In, First Out.
Think of a line at a coffee shop. The first person in line gets served first. New customers join the back of the line. No cutting - everyone waits their turn.
Real-World Queues
Queues appear everywhere in computing:
Printer jobs. When you print three documents, they print in the order you submitted them. The first document sent to the printer comes out first.
Message processing. A server receiving requests handles them in order. The first request received gets processed first.
Task scheduling. Background jobs run in submission order. Job submitted at 2:00 PM runs before job submitted at 2:01 PM.
The Queue Contract
A proper queue follows these rules:
- Items come out in the same order they went in
- You can only add to the back
- You can only remove from the front
- You can look at the front without removing it
You can't access items in the middle. You can't remove from the back. If you want the third item, you must remove the first two items to reach it.
Why FIFO Matters
FIFO order ensures fairness. Requests that arrive first get processed first. No newer request jumps ahead of older ones.
FIFO also maintains event ordering. If event A happens before event B, processing them in a queue ensures A completes before B starts.
Queue vs Other Structures
A queue is different from a regular list where you can access any position. With a queue, you only interact with the front and back. This restriction isn't a limitation - it's the point. The constraint ensures FIFO order.
A queue defines what operations you can do - enqueue at the back, dequeue from the front. This makes it an abstract data type. You can implement these operations using different underlying structures like arrays or linked lists. Both maintain FIFO order, but differ in how they handle adding and removing items. Later articles explore each implementation.
What's Next
Now that you understand the FIFO model, the next article explores the specific operations queues support: enqueue, dequeue, peek, size, and empty checks.