Understand Stack, LIFO, push, pop, peek, implementation, applications, complexity and important interview patterns.
A Stack is a linear data structure where insertion and deletion happen at one end called the top.
Stack follows Last In, First Out. The last element added is the first element removed.
Push, pop and peek are performed at the top of the Stack.
The core operations are push, pop, peek and checking whether the Stack is empty.
Like a pile of plates: add to the top and remove from the top.
Add an element to the top.
Remove the top element.
Read the top element without removing it.
Check whether the Stack is empty.
A Stack can be implemented using an array or a linked list.
| Operation | Complexity | Reason |
|---|---|---|
| Push | O(1) | Add at top |
| Pop | O(1) | Remove from top |
| Peek / Top | O(1) | Direct access to top |
| isEmpty | O(1) | Check current state |
Learn Stack concepts through visual explanations.