MainExamplesHistoryRecommended Reading
Explain Like I'm 5 /Computer Science

What is a Stack?

Help others learn from this page

A stack is a data structure that follows Last-In-First-Out (LIFO) — like a stack of plates. You add plates to the top and take them from the top. The last plate you put on is the first one you take off.

Operations:

  • Push: Add an item to the top
  • Pop: Remove and return the top item
  • Peek/Top: Look at the top item without removing it
  • IsEmpty: Check if the stack is empty

Why It's Useful:

  • Function calls: Programming languages use stacks to track function calls
  • Undo operations: Stacks remember what you did
  • Expression evaluation: Parsing mathematical expressions
  • Backtracking: Solving problems by trying and undoing

Real-World Analogy:

Think of a browser's back button. Each page you visit gets "pushed" onto a stack. When you hit back, it "pops" the most recent page.

FAQ

What's the difference between a stack and a queue?
Stack is LIFO (last in, first out). Queue is FIFO (first in, first out) — like a line at a store.
Can stacks overflow?
Yes! Stack overflow happens when you try to push too many items. This is why infinite recursion crashes programs.

Enjoyed this explanation? Share it!

Last Week in Plain English

Stay updated with the latest news in the world of AI, tech, business, and startups.

Interested in Promoting Your Content?

Reach our engaged developer audience and grow your brand.

Help us expand the developer universe!

This is your chance to be part of an amazing community built by developers, for developers.