← All Lessons Lesson 2 / 26
Lesson 2

The Queue: A Data Structure That Works in FIFO Order

The Queue: First In, First Out

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.

What a queue is

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:

  • You can add new items only at one end (the back).
  • You can remove items only from the other end (the front).

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.

A real-life queue: the ticket counter

Picture people standing in line to buy movie tickets:

  1. A new person who wants a ticket joins at the end of the line. They cannot cut in.
  2. The ticket counter only serves the person at the front of the line.
  3. When that front person gets their ticket, they leave from the front and walk away.
  4. The person who was second now becomes first, ready to be served next.

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.

From a real line to a data structure

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):

  • Start empty.
  • Add 1 → queue is 1
  • Add 2 → queue is 1 2
  • Add 3 → queue is 1 2 3
  • Add 4 → queue is 1 2 3 4

The newest value always lands at the back, just like a new person joining the line.

Removing items (this happens at the front):

  • Remove → 1 leaves first → queue is 2 3 4
  • Remove → 2 leaves → queue is 3 4
  • Remove → 3 leaves → queue is 4
  • Remove → 4 leaves → queue is empty

Notice 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.

Why this matters

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.

The Queue: A Data Structure That Works in FIFO Order
Diagram — click to zoom (scroll / drag to pan)