About Stack
What is a Stack ?
- stack is one-ended linear data structure which models a real world stack by having two primary operations, namely Push and Pop . and named Last In First Out
Instructions
- Explanation in an hour : ( 1:00:27 ) .
<aside>
<img src="/icons/light-bulb_gray.svg" alt="/icons/light-bulb_gray.svg" width="40px" />
pop( ); // remove the first element in the data
push( “Onion” ); // add in the first element in the data
push( “Celery” )
push( “Watermelon”)
pop( )
pop( )
push( “Lettuce” )
</aside>
When and Where is a Stack used ?
- Used by undo mechanisms in text editors.
- Used in compiler syntax checking for matching brackets and braces.
- Can be used to model a pile of books or plates.
- Used behind the scenes to support recursion by keeping track of previous functions calls.
- Can be used to do a Depth First Search ( DFS ) on a graph.
Complexity Analyses
Pushing |
O( 1 ) |
Popping |
O( 1 ) |
Peeking |
O( 1 ) |
Searching |
O( n ) |
Size |
O( 1 ) |