English dictionary, thesaurus, translations & etymology
FreeDict.com

stack vs queue

What's the difference between stack and queue? Here's the clear answer, with examples of each.

Quick answer

A stack works last in, first out — the last thing added is the first thing removed, like taking a plate off the top of a pile. A queue works first in, first out — whoever joined first gets served first, just like a line outside a shop. Picture a stack of plates versus a queue at the bakery and the order of removal tells you which is which.

The core difference

A stack removes the most recently added item first; a queue removes items in the order they arrived.

  • stack — a pile of things placed one on top of another, or the same idea in computing where the last item added is the first removed: each function call pushes a new frame onto the stack.
  • queue — a line of people or items waiting their turn, served in the order they arrived: the print job sat in the queue behind three other documents.

How to tell them apart

Both words describe things waiting to be dealt with, but the order is opposite. With a stack, picture a stack of plates: you always take the top one off first, so the last plate placed down is the first one removed. With a queue, picture people queuing at a bakery: whoever joined the line first gets served first, and nobody can jump ahead of someone who arrived earlier.

The words carry the same logic outside computing too. Stacking chairs or sandbags means piling them up, with the most recent addition on top and easiest to reach. Queuing for tickets or joining a queue means taking your place in line and waiting your turn in order.

Frequently asked questions

Which is LIFO and which is FIFO?
A stack is LIFO, last in first out; a queue is FIFO, first in first out.
What is a stack overflow?
It is an error that happens when too many items, or function calls, get pushed onto a stack and it runs out of space to hold them.
Does queue always mean computing?
No — its everyday sense is simply a line of people or vehicles waiting their turn, such as a queue outside a bakery.
Usage guides: FreeDict original editorial.