Summary

A stack is a data structure that follows one rule. Last in, first out. You add items to the top with a push and remove them from the top with a pop. The last item you put in is the first one you take out, like a pile of plates. Stacks power undo buttons, browser history and recursion.

A stack is one of the first data structures you meet after arrays. And it is one of the easiest to picture, because you use one every day. A pile of plates is a stack.

You add a plate to the top. You take a plate from the top. You never pull one from the middle. That single habit is the whole idea, and once it clicks, a surprising amount of computing makes more sense.

What is a stack data structure?

A stack is a collection of items where you only ever touch the top. You add to the top and you remove from the top. Nothing in the middle is reachable until you clear what sits above it.

This rule has a name. Last in, first out, often shortened to LIFO. The most recent item you added is always the next one to leave. Think of the plate pile again. The last plate you set down is the first plate you pick up.

That makes a stack different from an array, where you can reach any spot directly. A stack trades that freedom for order. It always knows exactly what comes next, and that turns out to be very useful. It is one of the core building blocks covered in any DSA learning path.

How does a stack work step by step?

Reading the rule is fine. Watching the top move is better. So let’s run a few operations on an empty stack and follow the top.

Start with an empty stack. Push the value 3. The stack now holds a single item, 3, and that is the top. Push 7 next. It sits on top of 3, so the stack reads 3 then 7, with 7 on top. Push 1. Now the stack is 3, 7, 1, and the top is 1.

Now pop once. A pop removes the top item, which is 1, so 1 leaves and the stack becomes 3, 7 with 7 back on top. Pop again. This time 7 leaves, and the stack is back to just 3.

See the order. The items came out 1 then 7, the exact reverse of how they went in. That reversal is the signature of a stack, and it is why stacks are so good at undoing things.

The main stack operations

A stack has a small, fixed set of moves. That is part of why it is fast and easy to reason about. There are four you need to know.

Push adds an item to the top. Pop removes the top item and hands it back. Peek, sometimes called top, shows you the top item without removing it. And isEmpty checks whether the stack has any items left, which matters because popping an empty stack is an error.

Stack code in Python and C++

In Python, a plain list already works as a stack. Here is a small class that names the operations clearly.

class Stack:
    def __init__(self):
        self.items = []

    def push(self, value):
        self.items.append(value)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()
        return None

    def peek(self):
        if not self.is_empty():
            return self.items[-1]
        return None

    def is_empty(self):
        return len(self.items) == 0

Each method maps to one operation. The append adds to the top, and the list pop removes from the top. C++ ships with a ready-made stack, which is what most Indian placement tests expect you to use.

#include <stack>
using namespace std;

stack<int> s;
s.push(3);          // add to top
s.push(7);
int t = s.top();    // peek, returns 7
s.pop();            // remove top, the 7
bool empty = s.empty();

The names differ a little, but the behaviour is identical. Add to the top, read the top, remove the top, and check if anything is left.

What is the time and space complexity of a stack?

A stack is fast because every operation touches only the top. Push, pop, peek and isEmpty each run in O(1) time. It does not matter if the stack holds ten items or ten million. Adding or removing one is the same quick step.

The space is O(n), where n is the number of items stored. Each item takes its own slot, so memory grows in step with how much you push. That simple, predictable cost is a big reason stacks show up everywhere. You can read the formal definition on Wikipedia’s stack page.

Where are stacks used in real programs?

Stacks are not just a textbook idea. They run quietly under tools you use all day. The clearest example lives inside your own code.

Every time a function calls another function, the computer pushes a frame onto a stack called the call stack. When a function finishes, its frame is popped off. This is exactly why recursion works the way it does, and why deep recursion can crash with a stack overflow when too many frames pile up.

The undo button in any editor is a stack. Each action you take gets pushed, and undo pops the last one off. Your browser’s back button works the same way, popping the most recent page to return you to the previous one. Stacks also check whether brackets in code are balanced and help turn math expressions into results.

The connection most students miss

The call stack is not just named after this data structure. It is one. When you finally understand a stack, recursion stops feeling like magic, because you can see the frames pushing on the way down and popping on the way back up. Learn the stack and recursion gets easier for free.

FAQ

What does LIFO mean in a stack?

Last in, first out. The most recently added item is the first one removed. It is the core rule that defines how a stack behaves.

What is the time complexity of stack operations?

Push, pop, peek and isEmpty all run in O(1) time, because each one touches only the top item. The space used is O(n) for n stored items.

What is the difference between a stack and a queue?

A stack is last in, first out, so you remove the newest item. A queue is first in, first out, so you remove the oldest item, like a line at a counter.

What happens if you pop an empty stack?

It is an error called stack underflow. Good code checks isEmpty before popping, and returns a safe value or a clear message instead of crashing.

Can a stack be built with a linked list?

Yes. You can build a stack from an array or a linked list. Both give O(1) push and pop. The choice depends on whether you want a fixed size or one that grows freely.

Why is the call stack called a stack?

Because it is one. Function calls are pushed on top and removed from the top in last in, first out order, exactly like any other stack.

So what should you remember?

A stack is the simplest rule in computing made useful. Add to the top, take from the top, and the newest item always leaves first.

That one habit powers undo buttons, back buttons, and the call stack that runs your every program. Master the stack and a lot of harder ideas suddenly have a floor to stand on.

Now picture this. You push 5, push 2, push 8, then pop once. Which value comes off, and what sits on top after?