Sat. Jan 21st, 2023


A last-in-first-out structure is called a stack. Think of a pile of plates in a canteen; the first one to be removed will be the plate at the top of the stack, which will have been the last one placed there.

Stack-specific terminology includes:

  • push – when an item it added to a stack, it is pushed onto the stack
  • pop – when an item is removed from a stack, it is popped off the stack

Stack structures are used in many different areas. For example:

  • When a recursive function is called, the stack is used to store the values of variables and other information such as the program counter, before another call to the function is made.
  • Performing complex calculations (those involving order of operation rules) often use a stack to process the values and keep track of current values
  • Undo functions in software record each change made onto the stack. In the event of a user wishing to undo actions, the actions are taken from the stack and reversed.