Stack Data Structure

Introduction to Stack

What is a Stack?

A Stack is a linear data structure that follows the LIFO (Last-In, First-Out) principle. The element inserted last is the first one to be removed. Think of a stack of plates: you add (push) and remove (pop) plates from the top.

Key Operations

  • Push: Add an element to the top of the stack
  • Pop: Remove the top element
  • Peek/Top: View the top element without removing it
  • isEmpty: Check if the stack is empty

Real-World Analogy

Imagine a stack of books. You can only add or remove the top book. The last book you placed is the first you can remove.

Stack Visualization

10
20
30
Top of Stack → Rightmost block
Stack Illustration