Data Structures ・ O(1)

A container where the last value pushed in is the first one popped out.

Move it step by step

Instruction list (runs left to right)

  1. push 3
  2. push 7
  3. push 5
  4. pop
  5. push 9
  6. pop
  7. pop
  8. pop
Value out
  • just pushed/enqueued
  • value out
  • empty slot
  • error
  • top is the topmost position

Step 1 of 9Instructions run: 0Values inside: 0

Running 8 instructions, one by one, on an empty stack. It holds up to 5 values.

The pseudocode line currently running

  1. 1push(x)
  2. 2if full, error (overflow)
  3. 3move top up by one, and place x there
  4. 4pop()
  5. 5if empty, error (nothing to return)
  6. 6return the value at top, then move top down by one

Change the example

Try predicting

You're on step 1.
What comes out on the next pop?

What you'll learn

  • The flow where the last value in is the first one out
  • Why taking a value out when it's empty is an error

Prerequisites

  • Arrays and indices (numbering that starts at 0)
  • How to add to the end of an array, and remove from the end

How a stack works

A stack is a container where you stack values up, and also take them out from the top. The last value pushed in is the first one to come out, so it's called last-in-first-out (LIFO).

Stacking is called push, and taking a value out is called pop. The position of the top value is called top, and it moves by one with every push and pop.

Steps, with an example

  1. 1For push, check that it isn't full, then place the value one above top
  2. 2For pop, check that it isn't empty, then return the value at top
  3. 3After returning it, move top down by one
  4. 4Treat a pop when empty, and a push when full, as errors
When you run the "Typical example" instructions in order
InstructionValue outContents (bottom to top)
push 3None[3]
push 7None[3, 7]
push 5None[3, 7, 5]
pop5[3, 7]
push 9None[3, 7, 9]
pop9[3, 7]
pop7[3]
pop3[]

Cost and complexity

OperationCostComplexity
push1 step (just place it on top)O(1)
pop1 step (just take the top one)O(1)
Looking at the bottom valuen steps (pop everything from the top, in order)O(n)

Push and pop only touch the top value, so the cost is the same whether there are 5 values inside or a million.

When to use it

Good fit

  • Undoing the most recent action (an undo history)
  • Checking that brackets match up
  • Keeping track of function calls (the call stack)

Not a good fit

  • When you want to process things in the order they arrived (use a queue instead)
  • When you need to repeatedly search for a value in the middle (use an array or a dictionary instead)

Code

Pseudocode

  1. 1push(x)
  2. 2if full, error (overflow)
  3. 3move top up by one, and place x there
  4. 4pop()
  5. 5if empty, error (nothing to return)
  6. 6return the value at top, then move top down by one

Working code

pythonRunnable
Output

▶ Press “Run” to see the result here

Edit the Python or JavaScript and press “Run” to see the result right away. Java is read-only here.

FAQ

Can a plain Python list work as a stack?
Yes. Push with append, and take the last item out with pop(), and that's already a stack. You only need to check the count, as shown above, if you want to cap how many it can hold.
What actually happens if you pop an empty stack in real code?
A Python list raises IndexError, and Java's ArrayDeque throws an exception. It's safer to check whether it's empty before popping.

Related