Stacks

A stack is a simple abstract data type with a small number of operations:

  • Push: put a new element on the top of the stack.
  • Pop: remove the top-most element on the stack.
  • Peek: take a look at the element on the top of the stack without removing it.

The key principle behind the stack is that there should be no means of accessing data at a lower level in the stack without first sequentially “pop”ing the elements above it in the stack. It has uses in numerous fields, including logic flow, image processing, and so on. A general purpose implementation is provided below. It is also referred to as a LIFO (Last In, First Out) structure, since the first element to be pushed onto the stack will be the last one popped off of it in normal circumstances.