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
| Feature | Stack (Plates) | Queue (Ticket Line) |
|---|---|---|
| Order | Last In, First Out (LIFO) | First In, First Out (FIFO) |
| Adding | Push onto top | Enqueue at back |
| Removing | Pop from top | Dequeue from front |
| Real-world | Undo, back button, function calls | Printer queue, customer service line |