What is a Stack?
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This means the last element added to the stack is the first one to be removed. Think of a stack of plates in a cafeteria: you add (push) a new plate to the top, and you remove (pop) the top plate first.
Time Complexity
- Push (Insertion)O(1)
- Pop (Deletion)O(1)
- Peek (Top Item)O(1)
- SearchO(n)
Real-World Applications
- ✓Function CallsCompilers use a call stack to keep track of active subroutines and recursion.
- ✓Undo MechanismsText editors store changes in a stack to allow users to undo their last actions.
- ✓Backtracking AlgorithmsUsed in maze solving (DFS) to remember the path taken.