Stacks


Conceptually a stack is similar to a stack of plates. When a plate is added it is added to the top of the stack. When a plate is taken from the stack it is grabbed from the top of the stack. A stack data structure works like a virtual version of a stack of plates. Elements are added to the top of the stack. Removing an element from the stack gets the last element that was added to the stack. A stack is often referred to as last in first out (LIFO). This refers to the order in which elements are added and removed from a stack.

Here’s an image depicting what adding an element to a stack looks like conceptually: StackAdd

Here’s an image depicting what removing an element from a stack looks like conceptually: StackRemove

Advantages/Disadvantages of Stacks:

Advantages:

  • fast access for adding/removing an element
  • essential for solving certain problems
  • can be implemented easily (similarly to a linked list)

Disadvantages:

  • limited use cases, not as generally useful as a list or array
  • unable to iterate through a stack

Data Structures