LIFO vs FIFO
The basic difference is simply the order in which items come out.
LIFO (Last In, First Out)
What goes in last comes out first.
- Used by a stack
- Operations:
push,pop
Example
Push: A → B → C
Pop: C (comes out first)
Real-life analogy
-
Stack of plates 🍽️
You put a plate on top → you take the top plate first.
FIFO (First In, First Out)
What goes in first comes out first.
- Used by a queue
- Operations:
enqueue,dequeue
Example
Enqueue: A → B → C
Dequeue: A (comes out first)
Real-life analogy
-
Line at a ticket counter 🎟️
The person who arrives first is served first.
Side-by-side comparison
| Feature | LIFO | FIFO |
|---|---|---|
| Full form | Last In, First Out | First In, First Out |
| Data structure | Stack | Queue |
| Removal order | Most recent item | Oldest item |
| Example use | Undo, function calls | Scheduling, printers |
Summary
LIFO = stack behavior (top comes out)
FIFO = queue behavior (front comes out)