Distinguish top from the rest
Updated
The top is one value. The rest is the stack you would have after one pop. You may look at the top. You may not address the third item down without popping or using a non-stack operation.
What to focus on
If you need a buried value, you pop until it is top, or you chose the wrong structure. A stack is not an array with extra names.
Worked examples
top vs under
a = [4, 1, 9]- top 9
- underneath [4, 1]
- 4 is not reachable by peek
Result: peek is 9 only
after one pop
a = [4, 1, 9], pop- 9 leaves
- new top is 1
Result: rest becomes the stack
one item
a = [7]- top is 7
- the rest is empty
Result: pop leaves []
Try it
Write topVsRest(a). a is the stack, written [bottom, ..., top]. Print the top, then the stack after one pop. You cannot peek 4 without first popping 9 and 1. On [4, 1, 9] top is 9 and after one pop the stack is [4, 1].
The stack is non-empty before each peek or pop. Values are integers.