A stack in data structures is a linear structure that follows the Last In, First Out principle, often called LIFO: the last item added is the first one removed. You can only insert or remove from one end, called the top.

How it works

Think of a pile of plates: you place new plates on top, and you also take plates from the top. That is exactly how a stack behaves in programming.

Common stack operations are:

  • Push : add an item to the top.
  • Pop : remove the top item.
  • Peek/Top : view the top item without removing it.
  • isEmpty / isFull : check whether the stack has no items or is at capacity.

Why it matters

Stacks are used in many places in computing, including undo/redo actions, browser history, and function calls in programs. They are popular because the rules are simple and the top-only access makes them efficient for these tasks.

Example

If you push 10, then 20, then 30 onto a stack, the first value you pop will be 30, then 20, then 10.

TL;DR: A stack is a data structure where the newest item is removed first, like a stack of plates.