Software DevelopmentData Structure And Algorithm

Stacks and Queues

Software Development May 18, 2026 3 min read
Stacks and Queues

What is a Stack (LIFO – Last In, First Out)?

Imagine a stack of plates in a cafeteria. You always take the top plate (the last one put on). You always put new plates on top.

Real-world examples:

  • Browser back button – The last page you visited is the first one you go back to

  • Undo (Ctrl+Z) – The last action you did is the first one undone

  • Call center phone queue (hold) – The last caller gets helped first? NO – that's actually wrong! Call centers use a queue, not a stack. Stacks are for "last come, first served" (like an elevator – last person in gets out first when the elevator is full and going down).

What is a Queue (FIFO – First In, First Out)?

Imagine a ticket counter line. The first person in line is the first person served. New people join at the back.

Real-world examples:

  • Printer queue – The first document sent is the first one printed

  • Customer service phone queue – The first caller waiting is the first one answered

  • Breadth-first search (finding shortest path on a map) – Explore closest locations first.

The Key Difference

FeatureStack (Plates)Queue (Ticket Line)
OrderLast In, First Out (LIFO)First In, First Out (FIFO)
AddingPush onto topEnqueue at back
RemovingPop from topDequeue from front
Real-worldUndo, back button, function callsPrinter queue, customer service line