Study resource
Stacks revision notes
Study Stacks with curriculum-aligned Revision Notes resources, practice links, and exam-focused support.
At a glance
revision notes
Resource type
Topic
Stacks
Revision notes
Stacks: Core Operations and Correct Application
What a stack operation does
A stack supports access through its top element. The operations in the specification are:
- push: adds a value to the top of the stack.
- pop: removes the top element from the stack. The removed value may then be used by the algorithm.
- peek or top: returns the value of the top element without removing it.
- test for empty stack: checks whether the stack currently contains no elements.
- test for stack full: checks whether the stack has reached the condition in which it cannot accept another element.
Applying the operations
Suppose the current stack, shown from bottom to top, is
[A, B]. Applyingpush(C)gives[A, B, C]. ApplyingpeekreturnsC, but the stack remains[A, B, C]. Applyingpopremoves and returnsC, leaving[A, B].Before using
pop, an algorithm should test whether the stack is empty. Popping from an empty stack is an invalid application of the operation because there is no top element to remove. Before usingpush, an algorithm can test whether the stack is full. If the stack is full, the algorithm should not attempt to add another value.Common errors
Do not describe
peekas removing the top value: it only returns that value. Do not confusepopwith inspecting the top:popchanges the stack by removing its top element. When tracing operations, update the stack after everypushandpop, but not afterpeek. Also distinguish the two tests: empty concerns whether there are no elements, whereas full concerns whether another element can be added.
