Skip to content

Stacks

Last in, first out. One end, three operations, and the structure behind undo, browser history and every function call your program makes.

Class 11 to 12beginner20 min1 lesson1 interactive lab
By the end you will be able to
  • State the LIFO rule and why it constrains access to one end
  • Perform push, pop and peek
  • Recognise stacks in real systems, including the call stack

Finish first

Cost at a glance

push
O(1)
pop
O(1)
peek
O(1)
search
O(n)

Lesson 1 of 1

One end only

A stack is deliberately restrictive: you may only add to the top and only remove from the top. That is LIFO, last in, first out. The restriction is the feature, because it makes every operation O(1) and makes the structure impossible to misuse.

You have used one all day without noticing. Ctrl+Z undoes your most recent action first. The browser back button returns to the page you visited most recently. And when a function calls a function, the call stack remembers where to return, the deepest call finishes first.

Interactive lab

Stack operations

Push, pop and peek on a vertical stack, with the top pointer always visible.

Stack: last in, first out

push / pop / peek all O(1)
top ↓
5
10
top
2 items
1 / 2

Start

Peek reads the top item without removing it.

Continue

Worked examples

Read the code, then change it

Copy any example into the playground and break it on purpose. That is the fastest way to learn what each line is holding up.

Balanced brackets: the classic stack problemJavaScript
function isBalanced(text) {
  const pairs = { ')': '(', ']': '[', '}': '{' };
  const stack = [];
  for (const char of text) {
    if ('([{'.includes(char)) stack.push(char);
    else if (char in pairs) {
      if (stack.pop() !== pairs[char]) return false;
    }
  }
  return stack.length === 0;
}

console.log(isBalanced('{[()]}'), isBalanced('{[(])}'));

Practice

Work these out yourself

No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.

0/3 attempted

Assessment

Check your understanding

Answer each question, then read the explanation. That is where the learning is.

0/5
  1. Question 1: You push 5, 10, 15 and then pop once. What does peek return?
    Question 1 / 5

    You push 5, 10, 15 and then pop once. What does peek return?

    Select an option first
  2. Question 2: Which real system is a stack?
    Question 2 / 5

    Which real system is a stack?

    Select an option first
  3. Question 3: Why are push, pop and peek all O(1)?
    Question 3 / 5

    Why are push, pop and peek all O(1)?

    Select an option first
  4. Question 4: Checking `{[(])}` with the bracket-matching algorithm fails at which point?
    Question 4 / 5

    Checking `{[(])}` with the bracket-matching algorithm fails at which point?

    Select an option first
  5. Question 5: A stack overflow error means:
    Question 5 / 5

    A stack overflow error means:

    Select an option first
5 of 5 questions left.

Finished this topic?

0 of 1 lessons marked done. Marking the topic complete ticks the rest.