The diagram shows the initial contents of a stack, with 5 at the top.
The following operations are performed in order: push(6), pop(), pop(), push(1), peek().
(a) State the values returned by the two pop() operations. [1]
(b) State the value returned by peek(). [1]
(c) Write down the final contents of the stack from bottom to top and state the number of items. [2]
- (a) 6 then 5(1)
- (b) 1(1)
- (c) Final contents bottom to top: 8, 3, 1(1)
- 3 items(1)
- Answer: (a) 6, 5 (b) 1 (c) 8, 3, 1 – three items
- Examiner tip: A stack is last-in first-out: the first pop returns the value just pushed (6), not the original top.