Saltar al contenido

Lesson 77: Stack

English

Last in, first out

  • A stack is like a pile of plates: you add (push) and remove (pop) only at the top.
  • The stack pointer sp shows the next free place. Push stores at sp and adds 1; pop subtracts 1 and takes that item.
  • Check before you push (full?) and before you pop (empty?).
After Places 1 2 3 4 sp
start – – – – 1
push 3 3 – – – 2
push 8 3 8 – – 3
pop → 8 3 – – – 2

A stack after push 3 and push 8: place 1 holds 3, place 2 holds 8 (the top), places 3 and 4 are empty; sp is 3, the next free place. Push stores at sp, then adds 1; pop subtracts 1, then takes that item

  • Robots lesson 47 has the opposite: a queue, first in, first out.

Iniciar sesión o crear cuenta

IGCSE, A-Level & AP