What is Stack Foundation
A stack is a list where the last value pushed is the first value popped. The open end is the top.
Approach
Visualization & algorithm
The finished picture is on screen when the page opens. Next and Play start from the first step. The controls sit under the picture and the pseudocode.
Complexity
| Piece | Cost | Why |
|---|---|---|
| Push or pop | O(1) | Only the top changes. |
| Space | O(n) | Every pushed value is stored until it is popped. |
Implementation
stack = [] stack.append(1) # push stack.append(2) top = stack[-1] # peek stack.pop() # pop