Real-Life Examples of Stack Operations

TIME : 2016-01-07
Real-Life Examples of Stack Operations

A stack is last-in, first-out: the last thing you put on is the first thing you take off. That pattern turns up constantly outside of computing.

  • Function calls. The call stack tracks which procedure is running so the program knows where to return.
  • Recursion. Each recursive call is pushed onto the stack and popped off as it resolves.
  • Bangles. The last one you slide on is the first one you take off.
  • A stack of plates. You add and remove from the top, so the first plate down is the last one up.
  • Batteries in a torch. You cannot reach the second battery until the first is out.
  • Cars in a garage. The car parked first has to wait until everything behind it has left.
  • Clothes in a suitcase. Whatever you packed last is what you unpack first.
  • CDs in a case. Same idea, one disc at a time from the front.

Undo history in a text editor works the same way, which is why you undo your most recent change first.