A stack is a collection of items where the last thing you add is the first thing you take out. That single rule, called last-in-first-out (LIFO), is the whole idea. You add an item by pushing it onto the top, and you remove an item by popping it off the top, so you never reach into the middle. Stacks are everywhere in software, from the call stack that tracks your running functions to the undo button in your editor. Here is how a stack works, where it shows up, and when it is the wrong choice.
How a stack works
Picture a stack of plates. You put a clean plate on top, and when you need one, you take the top plate. You do not pull a plate from the middle, because the rest would topple. A software stack behaves the same way: items go on and come off the same end, called the top.
There are really only two operations you need to know. Push places a new item on top. Pop removes the top item and hands it back. Many stacks also offer peek, which lets you look at the top item without removing it. Because every action happens at the top, a stack is fast and predictable, which is part of why it is so common.
If the underlying ideas of items, types, and structure are new to you, our explainer on what a data type is gives helpful background before you go further.
The call stack
You use a stack every time you run a program, even if you never write one yourself. When a function calls another function, the language records where it was so it can return later. That record sits on the call stack. The most recent call is on top, and when it finishes, the program pops back to whatever called it.
This is why a runaway chain of function calls that never stops eventually crashes with a stack overflow: the call stack runs out of room. Understanding this helps you read error messages, because the stack trace you see is literally the contents of the call stack, top to bottom.
Stack vs queue
The most common confusion is between a stack and a queue. A queue is first-in-first-out: the oldest item leaves first, like a line at a coffee shop. A stack reverses that. Choosing the wrong one quietly breaks your ordering.
| Feature |
Stack |
Queue |
| Order |
Last in, first out |
First in, first out |
| Add to |
Top |
Back |
| Remove from |
Top |
Front |
| Real analogy |
Plate stack |
Checkout line |
| Good for |
Undo, back button, parsing |
Job processing, fair scheduling |
How to use a stack well
- Reach for a stack when reverse order is natural, such as undo history or evaluating nested brackets.
- Use push and pop only at the top; if you find yourself wanting the bottom item, you likely want a different structure.
- Watch the depth when recursion is involved, since deep call chains can exhaust the call stack.
- Prefer your language built-in list or stack type rather than rolling your own, because it is tested and fast.
Common mistakes
- Using a stack when you need fairness. First-come-first-served work belongs in a queue, not a stack.
- Reaching into the middle. If your logic needs the third item down, a stack is the wrong tool.
- Ignoring depth limits. Unbounded recursion overflows the call stack and crashes the program.
- Confusing the data structure with the website. A stack the concept is unrelated to any site that happens to share the name.
FAQ
What does LIFO mean?
LIFO stands for last-in-first-out. The most recently added item is the first one removed, which is the defining rule of a stack.
What is the difference between a stack and a queue?
A stack removes the newest item first; a queue removes the oldest first. Use a stack for undo or back-button behavior and a queue for fair, in-order processing.
What causes a stack overflow?
The call stack runs out of space, usually because a function keeps calling itself without a stopping condition. Adding a proper base case fixes most of these.
Is a stack hard to learn?
No. A stack has only a few operations, push, pop, and peek, which makes it one of the friendlier data structures to pick up early.
Where to go next
Build on the basics with What Is a Data Type in 2026, see how repetition works in What Is a Loop in Programming in 2026, and learn to decode failures in What Is a Syntax Error in 2026.