Stack
A stack can hold several objects. Add each new object on top. You can only look at or take the top object, the one you added most recently among those still there.
Imagine walking through a maze and writing down your turns. Left, left, right, straight, right… A dead end! Turn around and go back to the last junction. You turned right there on your way in. Turn left on your way back and cross out the last “right”.
To get back to the start of the maze, read your notes from the end. At each junction, you need the last note. It tells you how to get back to the previous stretch of the path.
Stacks come up often in programming. You can use a list in Python or an array in JavaScript.
Three stack operations
push— Put a new object on top.pop— Take the top object off the stack.peek— Look at the top object. It stays on the stack.