Stacks

The notes

Core concept

  • LIFO principle (last-in, first-out)
  • Elements are accessed only from one end (the top)
  • The last thing I put here is the first thing I get
  • Pictured as a stack of plates with a top and a bottom

Key operations

  • push(item): add an element to the top
  • pop(): remove the top element
  • peek(): view the top element without removing it

Real-world examples

  • Undo / redo: actions and undo history
  • Function calls (call stack): managing function execution and recursion (main(), func1(), func2(), func3() pushed and popped)
  • Browser history: back and forward through pages
  • Expression parsing: converting (A+B)*C to postfix notation

Data structures deck

  1. 1. Linear vs non-linear
  2. 2. Arrays
  3. 3. Hashmaps
  4. 4. Stacks
  5. 5. Queue
  6. 6. Linked list
  7. 7. Binary tree
  8. 8. BST
  9. 9. Graphs
  10. 10. Heaps

Watch the lesson

Data structures, Lesson 3, from 1:20: How does a stack look and behave?