A stack: last in, first out
Think of a pile of plates. You put a plate on top, and you take a plate from the top. The last plate you put down is the first one you pick up. An undo button works the same way: it takes back your newest change first.
Push: puts an item on top.
Pop: takes the top item off. Both happen at the same end.
Actions [3, 8, 0, 6, 2, 0], where 0 means undo
3
08
10
26
32
40
5i
3: push it. Stack: [3].
Move 1 of 6
The stack in each language
In Python and JavaScript, a plain list is a stack: append (push in JS) adds at the end, and pop removes from the end. Both take about one step. C++ has stack<int> with push, pop and top. In Java, use ArrayDeque with push, pop and peek. Popping an empty stack is an error, so check first.
Python list · JS array: append / push and pop, at the end.
C++ stack · Java ArrayDeque: push, pop, and top / peek.
Check before you pop: an empty stack has nothing to take.