What is a Stack?
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Think of it like a stack of plates in a cafeteria: you add new plates to the top, and when you need a plate, you take from the top. The last plate you added is the first one you remove.
Real-World Analogies:
- •Stack of plates: add/remove only from the top
- •Browser back button: most recent page is accessed first
- •Undo functionality: most recent action is undone first
- •Function call stack: most recent function returns first
The LIFO Principle: Whatever you put in last comes out first. This is fundamentally different from a queue (FIFO - First-In, First-Out) where items come out in the order they went in.
This reversal property is extremely useful for many algorithmic problems.