Some problems can only be solved correctly when we handle items in the exact order they arrived — the first item that comes in must be the first one to go out. This rule has a name: FIFO, short for First In, First Out. To follow this rule cleanly, we use a data structure that is built around it from the start. That data structure is called a queue.
A queue is a linear container — think of it as a straight line of items. What makes it special is that it has two open ends, and each end has a fixed job:
Because adding and removing happen at opposite ends, the item that entered first is always the one closest to the exit. So it leaves first. This is exactly the FIFO order.
Picture people standing in line to buy movie tickets:
Nobody jumps ahead, and nobody is served out of turn. The first person to join is the first person to be served and the first to leave. That is FIFO in everyday life.
When we build this same idea inside a programming language — to store data items and retrieve them in FIFO order — we call it the queue data structure. It is the computer's version of the line of people.
For example, imagine a queue holding the numbers 1, 2, 3, 4, where 1 joined first and 4 joined last.
Adding items (this happens at the back):
1 → queue is 12 → queue is 1 23 → queue is 1 2 34 → queue is 1 2 3 4The newest value always lands at the back, just like a new person joining the line.
Removing items (this happens at the front):
1 leaves first → queue is 2 3 42 leaves → queue is 3 43 leaves → queue is 44 leaves → queue is emptyNotice the order things came out: 1, 2, 3, 4 — the same order they went in. That is the whole point. The queue forced FIFO for us automatically; we did not have to track who arrived first by hand.
By restricting where we add and where we remove, the queue mimics the FIFO order of a real-world line by design. We get the correct order for free, simply because of how the structure is built.
A queue is one of the core data structures in computer programming. It quietly powers many systems — like jobs waiting to be printed, tasks waiting for the processor, or messages waiting to be handled — anywhere items must be dealt with strictly in the order they arrived. As the course continues, we will learn more about how to build and use this powerful structure.