aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer ScienceTopic 6.2Stacks (LIFO)
Back to Computer Science Topics
6.2.35 min read

Stacks (LIFO)

IB Computer Science · Unit 6

Smart study tools

Turn reading into results

Move beyond passive notes. Answer exam-style questions, get AI feedback, and build the skills that earn top marks.

Get Started Free

Contents

  1. 1The most recent one comes back first
  2. 2The four operations
  3. 3What stacks are actually for
  4. 4Exam-style question
The big idea: A stack only lets you add and remove at one end — the top.

So the last item put in is the first one taken out. That is what LIFO means.

The same three items into a stack and a queue, then taken back out. Watch the orders diverge.

Interactive diagram

Explore the labelled diagram, charts and maps for this topic in full study mode.

Claim your free topic

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime

push and pop

  • push(item) — put one on the top
  • pop() — take the top one off, and return it
  • Both touch only the top, so both are O(1)
  • Nothing is shifted or searched for

peek and isEmpty

  • peek() — look at the top without removing it
  • isEmpty() — is there anything in it at all?
  • Always check isEmpty before popping
  • Popping an empty stack is an error

What you cannot do

  • Reach the middle — the top is the only way in
  • Search it without emptying it
  • That restriction is the point, not a shortcoming
stack = []

stack.append("A")     # push
stack.append("B")     # push
print(stack[-1])      # peek  → B
print(len(stack) == 0)  # isEmpty → False

top = stack.pop()     # pop → B
print(stack)          # ['A']
Stack<String> stack = new Stack<>();

stack.push("A");                        // push
stack.push("B");                        // push
System.out.println(stack.peek());       // peek  → B
System.out.println(stack.isEmpty());    // isEmpty — false

String top = stack.pop();               // pop → B
System.out.println(stack);              // [A]
Why the restriction helps: Because every operation touches only the top, a stack is fast and predictable: no shifting, no searching, constant time however big it grows.

A structure that can do less can guarantee more.

Asked to sketch a stack? Draw one column per operation, newest item on top, and mark the top every time.

A stack sketched column by column as items are pushed and popped
How to sketch a stack after each operation
1

Reaching the bottom item

Only the top can be reached. To find the FIRST item pushed, pop until isEmpty is true, keeping the last item popped: that is the bottom one.

2

Keeping the stack intact

Push each popped item onto a second stack instead of throwing it away, then pop them all back to restore the original order.

Get feedback like a real examiner

Submit your answers and get instant feedback — what you did well, what's missing, and exactly what to write to score full marks.

Try AI Tutor FreeYour first topic is free to keep • No credit card required
1

Undo

Every action is pushed. Undo pops the most recent one — which is exactly what a person expects "undo" to mean.

A queue here would undo your first action, which would be useless.

2

The call stack

When a function calls another, where to return to is pushed. When it finishes, that is popped.

Calls must unwind in reverse order, so a stack is the only structure that fits.

3

Checking brackets match

Push every opening bracket; on a closing one, pop and check it matches.

The most recent opening bracket is always the one that must close first — LIFO exactly.

4

Going back

Browser history: each page visited is pushed, and Back pops the most recent.

Any "most recent first" behaviour is a stack.

Stack overflow: A stack that keeps growing without anything being popped eventually runs out of memory.

The usual cause is a function that calls itself with no way to stop — which is where the phrase stack overflow comes from.

How this is tested — you must justify LIFO from the scenario, and trace pushes and pops correctly. It comes up two ways:

Paper 2 — working with code

  • Explain a stack and its operations, 3-5 marks
  • Trace a sequence of pushes and pops
  • Say why a stack suits a described problem

Paper 2 — the algorithmic-thinking question

  • Choose between a stack and a queue from behaviour alone
  • Explain what would go wrong with the other one
The classic trap: Naming the structure without justifying it. The mark is for saying why the most recent item is the one wanted — undo, unwinding calls, matching brackets.
IB-style questionExplain[5 marks]

A text editor must support undo. Explain why a stack is the right structure, naming the operations used.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on Stacks (LIFO). Write your answer and get instant AI feedback — just like a real IB examiner.

A stack supports push, pop, peek and isEmpty. the stack that results from this sequence, clearly indicating the top: push(4), push(7), push(2), peek(), pop(), push(5), push(9), pop(), push(3). [2 marks]

Related Computer Science Topics

Continue learning with these related topics from the same unit:

6.1.1Variables and data types6.1.2Substrings6.1.3Exception handling6.1.4Debugging
View all Computer Science topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science
Previous6.2.2Arrays and listsNextQueues (FIFO)6.2.4

26 exam-style questions ready for you

Students who practice on Aimnova improve their scores by 15% on average. Get instant feedback that shows exactly how to improve your answers.

Practice Now — FreeView All Computer Science Topics