Skip to content

Stacks

Implement a stack with push, pop and peek on a Python list, each in O(1), explain why its top is the end of the list, and use it to check that brackets are balanced.

It opens on

Three items are put on a pile one after another, 4, then 9, then 2, and only the top item can ever be taken. In what order do they come off?

Why it matters

The call stack that runs every program, the undo history of an editor and the check that a compiler makes on parentheses are all stacks, because the thing most recently put aside is always the next thing wanted.

Good to know first

  • Lists: indexing and mutation: append adds at the end, pop() removes and returns the last item and items[-1] reads it
  • Dynamic arrays and amortized cost: append is O(1) amortized and inserting at the front is O(n)
  • Doubly linked lists and sentinels: add and remove at an end are O(1) operations chosen by which nodes are touched

Step by step

  1. Last in, first out

    Use push, pop and peek on a stack, say the order in which items leave, and write the Stack class over a Python list.

    A pop here takes the 2, because it went on last, and the top is now the 9.

    A common mix-up

    Peek and pop do the same thing, since both give the top.

    In fact: Peek leaves the item on the stack and pop removes it, so a second pop after a peek returns the same item and after a pop it returns the one below.

  2. Which end is the top

    Count the items moved by n pushes with the top at the front of a list and with the top at the end, and conclude that the end is the right top.

    Push four items with the top at the front. The first push finds 0 items, the second 1, the third 2 and the fourth 3, so 0 + 1 + 2 + 3 = 6 items are moved.

    the row for 4 pushes

    A common mix-up

    The top could be either end of the list at the same cost.

    In fact: At the front each push or pop moves every item, O(n), while at the end each is O(1) amortized.

  3. Balanced brackets

    Check that a string of brackets is balanced by pushing each opener and popping a matching one at each closer, and trace it on a valid and an invalid string.

    Trace {[()]}. The characters {, [ and ( are openers, so each is pushed, and the stack grows to { [ (.

    the first three rows of the trace

    A common mix-up

    A string is balanced when it has as many closers as openers of each kind.

    In fact: ([)] has one of each kind and is not balanced, since the order matters, and only a stack that is checked at every closer sees it.

Try it

A stack gets push(6), push(1), push(4), then pop(), then push(5). What does peek return, and what is the whole stack from bottom to top?Answer

5; the stack is 6, 1, 5

Hint: Pop() removes the 4

How many items does pop(0) shift in all when 6 items are popped one after another from a list holding 6 items?Answer

5 + 4 + 3 + 2 + 1 + 0 = 15

Hint: The list shrinks by one after each pop

What does balanced return for the string [{}]), and at which character does it decide?Answer

False, at the last character, ), since the stack is empty by then

Hint: [ and { are pushed, } and ] pop them

If we keep on popping, what comes off next, and in what order do 4, 9 and 2 leave in all?Answer

The 9 and then the 4, so they leave as 2, 9, 4, the reverse of the order they went in.

How many moves do 100 pushes make with the top at the front?Answer

0 + 1 + ... + 99 = 4950, which is n(n − 1)/2 for n = 100.

Stacks

30 minutes every month. No card.