What are queues and stacks?

A queue is like a line at the cafeteria, and a stack is like a tower of blocks you build one on top of the other.

What’s a Queue?

Imagine you’re waiting in line for ice cream. The first person gets their ice cream first, then the next person, and so on, just like how a queue works. People join the end of the line, and when it's their turn, they move forward. That’s called first-come, first-served.

What’s a Stack?

Now think about stacking blocks. You put one block on top of another. The last block you add is the first one that will fall if you knock it over, that’s how a stack works too! In a stack, things are added and removed from the top, like when you take off your socks, then your shoes, then your pants.

In both cases, we're organizing things in order, but in different ways. A queue is like a line, and a stack is like a tower!

Take the quiz →

Examples

  1. A queue is like a line at a coffee shop, people join the end and get their turn one by one.
  2. A stack is like plates in a kitchen, you place new plates on top, and take them off from the top too.
  3. In a video game, enemies might be queued up for a boss fight, while your inventory uses a stack.

Ask a question

See also

Discussion

Recent activity