LIFO: Stacks

A Stack is a linear data structure that follows a specific order in which operations are performed. The order is LIFO (Last-In, First-Out)—meaning the last element added to the stack is the first one to be removed.


1. Core Operations

A stack is a restricted data structure; you can only interact with the "Top" element.

All core operations are O(1) (Constant Time).


2. Implementations

I. Array-based Stack

Uses a dynamic array to store elements.

II. Linked List-based Stack

Each element is a node pointing to the one below it.


3. Practical Applications

  1. The Function Call Stack: Every time a programming language calls a function, a "Frame" containing local variables and the return address is Pushed onto the system stack. When the function returns, the frame is Popped.
  2. Undo/Redo Logic: Every action in a text editor is pushed onto a stack. "Undo" simply pops the last action.
  3. Expression Evaluation: Parsing mathematical expressions (Reverse Polish Notation) and balancing parentheses (([])).
  4. Backtracking: Storing the state while exploring paths in algorithms (like DFS).

Interview Pro-Tips: The Stack Overflow

A common interview topic: "What is a Stack Overflow?"


Technical Summary

  1. Vertical: Think of a stack of physical plates.
  2. O(1): Efficient access to the most recent data.
  3. Context: Used heavily by compilers and runtimes for flow control.