Push onto a nonempty stack
Updated
On a nonempty stack, push does not look at buried values. It only installs a new top. The old top becomes second-from-top.
What to focus on
Size n becomes n+1. Peek changes. The bottom does not change. You never rewrite index 0 to push.
Worked examples
three to four
a = [4, 1, 9], push 8- bottom stays 4
- top 9 → 8
Result: [4, 1, 9, 8]
one to two
a = [7], push 3- 7 is now buried
Result: [7, 3]
repeat value
a = [4, 1, 9], push 9- two 9s
- only the new 9 is top
Result: [4, 1, 9, 9]
Try it
Write push(a, x). a is the stack, written [bottom, ..., top]. x is the integer to add as the new top. Print bottom and top after each push. Bottom must be unchanged. Push 8 onto [4, 1, 9]: bottom 4, top 8. Push 3 onto [7]: bottom 7, top 3.
The stack is non-empty. Values are integers. Capacity is enough for one more item.