- is a linear DS that works like a box, i.e one end is closed
- insertion operation is called push operation
- removal is called pop
- follows Last in first out order (LIFO)
- isEmpty() or empty(): return true if stack is empty, else false
- push(x): inserts an item to top of stack
- pop(): removes an item from the top
- peak() or top(): returns the top item
- size(): returns size of stack
- underflow: when pop() or peek() function is called on empty stack
- overflow: when push called on a full stack
- Array implementation of stack
- Linked List implementation of stack
- Stack in C++ STL
- by defualt STL implements stack using deque container (container adapter: built on top of other containers, work as an interface)
- function calls
- checking for balanced parentheses
- reversing items
- infix to prefix/postfix conversion
- evaluation of postfix/prefix
- stock span problem and it's variations
- undo/redo cmd
- since stack follows LIFO, whatever was done last, has to be undone
- and whatever was undone last has to be re-do
- forward/backward in web-browsers
-
[Kstacks in an array]
- damn idk, I didn't understand
-
- dry run image
- Next Greater Element
- dry run image
-
Largest Rectangular Area in a Histogram
- dry run
-
rule for exp
operator | associativity ^
^ (power)| R to L | precedence
*, / | L to R |
+, - | L to R |
if there are two operators
and you need to decide which operator to evaluate first
you look at precedence table
but if two operators have same precedence
then look at associativity
- Infix to Postfix conversion
- Evaluation of Postfix expression
- Infix to Prefix conversion
- Evaluation of Prefix expression
- Reverse stack els using recursion
- Usually used for optimising your O(N^2) idea with a tradeoff for space
- when you need to play with greater/smaller indices
- keeping a track of certain kind (next greater/prev greater) of elements