Stacks (Cambridge International A-Level Computer Science)
Exam board Cambridge International · A-LevelUpdated 2025-06-03
Implementing a stack
How do you program a stack?
- To recap the main operations of stacks and how they work, read the ADT overview
- When programming a stack, the main operations listed below must be included in your program.
- The main operations for stacks are:
Main operations
| Operation | Pseudocode | Python | Java |
|---|---|---|---|
| isEmpty() | FUNCTION isEmpty(s : Stack) RETURNS BOOLEAN RETURN s.top = -1 ENDFUNCTION | def isEmpty(self): return len(self.stack) == 0 | stack.isEmpty(); |
| isFull() | FUNCTION isFull(s : Stack) RETURNS BOOLEAN RETURN s.top = s.capacity - 1 ENDFUNCTION | def isFull(self): return len(self.stack) == self.capacity | public boolean isFull() { return top == capacity - 1; } |
| push(value) | PROCEDURE push(s : REF Stack, value : INTEGER) IF NOT isFull(s) THEN s.top ← s.top + 1 s.items[s.top] ← value ELSE OUTPUT "Stack is full" ENDIF ENDPROCEDURE | stack.push(1) | stack.push(1); |
| pop() | FUNCTION pop(s : REF Stack) RETURNS INTEGER IF NOT isEmpty(s) THEN DECLARE value : INTEGER value ← s.items[s.top] s.top ← s.top - 1 RETURN value ELSE RETURN -1 ENDIF ENDFUNCTION | popped_item = stack.pop() | int poppedItem = stack.pop(); |
| peek() | FUNCTION peek(s : Stack) RETURNS INTEGER IF NOT isEmpty(s) THEN RETURN s.items[s.top] ELSE RETURN -1 ENDIF ENDFUNCTION | def peek(self): if not self.isEmpty(): return self.stack[-1] else: return None | int topItem = stack.peek(); |
| size() | FUNCTION size(s : Stack) RETURNS INTEGER RETURN s.top + 1 ENDFUNCTION | size = len(stack) | int size = stack.size(); |
- In pseudocode, we assume a record-based stack with fields like
items,top, andcapacity - Python uses built-in lists or a class with
.push()and.pop()methods - Java uses
Stack<E>fromjava.util.Stack
Stack in operation (Pseudocode)
-
Defines a Stack structure with a fixed-size array of 5 items, a
toppointer, and acapacity -
Initialises the stack with
top ← -1to indicate it is empty -
Pushes 3 values (
10,20,30) onto the stack one by one:- Each
pushincreasestopand adds the value to theitemsarray
- Each
-
Peeks at the current top of the stack:
- Returns
30without removing it
- Returns
-
Pops two values off the stack:
- First pop returns
30, second returns20 - Each
popdecreasestopby 1
- First pop returns
-
Outputs the current size of the stack:
- After popping twice, only one value (
10) remains → size is1
- After popping twice, only one value (
Python
Java
Worked Example Study the pseudocode function
PushAnimal():Write program code for the function
PushAnimal()[3]Final answer: Answer
- Function header (and close where appropriate) with parameter, checking if full (
AnimalTopPointer = 20) and returning false [1 mark]- If not full, inserting parameter value into
AnimalTopPointer[1 mark]- …incrementing pointer and returning true [1 mark]
Example program code:
Java
VB.NET
Python
Was this revision note helpful?
Build on this topic