Programming Companion
Java

Programming technique · B2.2.3

Stacks

A stack restricts access to one end, called the top. The most recently pushed item is therefore the first item available to inspect or remove.

IB DP CS standard B2.2.3: Explain stacks as last in, first out structures, including push, pop, peek, isEmpty, qualitative effects on performance and memory, and suitability for a specific problem.
Last in, first out (LIFO) Resize title was pushed last, so it is now at the top and will be removed first.
Draw circle
Add colour
Resize title

The four required operations

push

Add one item at the top.

pop

Remove and return the top item.

peek

Return the top item without removing it.

isEmpty

Check whether a top item exists.

Using Java's supplied stack operations

Java can represent a stack with a Deque backed by ArrayDeque. The collection is supplied; students do not need to implement the internal structure.

Using a Python list as a stack

A Python list supplies the required end operations: append() pushes, pop() removes the top, stack[-1] peeks and len(stack) == 0 checks whether it is empty.

Abstract operationJava callReturned valueStack after operation, top firstStored count
push("Draw circle")history.push(...)Draw circle1
push("Add colour")history.push(...)Add colour, Draw circle2
peek()history.peek()Add colourAdd colour, Draw circle2
pop()history.pop()Add colourDraw circle1
isEmpty()history.isEmpty()falseDraw circle1
Abstract operationPython callReturned valueStack after operation, top firstStored count
push("Draw circle")history.append(...)Draw circle1
push("Add colour")history.append(...)Add colour, Draw circle2
peek()history[-1]Add colourAdd colour, Draw circle2
pop()history.pop()Add colourDraw circle1
isEmpty()len(history) == 0FalseDraw circle1

Performance and memory, explained qualitatively

Direct top access

The stack abstraction works only at the top. It does not search for or remove an arbitrary middle item.

Stored memory changes

push stores one more item and pop stores one fewer. peek and isEmpty do not change the stored count.

Check before removal

An empty stack has no top item. Check isEmpty before calling pop or relying on peek.

Check before removal

An empty list has no final item. Check the length before calling pop() or reading stack[-1].

Suitability question Does the most recently added item need to be handled first? If yes, a stack may be appropriate. An oldest-request-first service is not a stack problem.
Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Stack Operation Trace

Challenge ID: PC-T11-C01 · Standards: B2.2.3

Collect the stack tracing sheet from the front of the class. Before running the supplied Java program, trace every operation by hand. Record the returned value, stack state from top to bottom and stored-item count after each step. Hand the completed tracing sheet to your teacher, then run the program and correct any mistakes in a different colour. Explain why peek leaves the stack unchanged. Do not implement a custom stack.

Scaffold available
Items A, B and C being pushed, inspected and popped while a stack trace changes step by step.

Undo History

Challenge ID: PC-T11-C02 · Standards: B2.2.3, B2.3.2, B2.3.4

Use a Deque as a stack of action names. Provide options to add an action, view the next action that would be undone, undo the latest action and report when no actions remain. Keep stack operations in clear methods and add a short explanation of why last in, first out matches undo behaviour. Do not build your own stack class.

Scaffold available
Recent actions arranged as a last-in-first-out undo history with the newest action removed first.

Bracket Checker

Challenge ID: PC-T11-C03 · Standards: B2.2.3, B2.3.2, B2.3.3

Use a Deque<Character> as a stack to decide whether round, square and curly brackets are balanced. Push opening brackets, compare each closing bracket with the top item and reject an unmatched or incorrectly ordered pair. Test nested, adjacent, missing and extra brackets. Explain why a stack is suitable.

Scaffold available
Round, square and curly brackets being matched with one incorrectly ordered bracket highlighted.