Stack Operations

You already know a stack works like a pile of plates. You add a plate on top. You take a plate from the top. But how do you actually do that in code? You need a small set of operations. Every stack gives you these. Learn them once and you can use a stack in any language. So let us go through them one by one.

🎯 The Operations You Need

A stack only lets you touch the top. That sounds limiting at first. But that one rule is also what makes a stack fast and simple. Here are the operations that make a stack work.

  • push β€” put a new item on top of the stack.
  • pop β€” remove the top item and give it back to you.
  • peek (also called top) β€” look at the top item without removing it.
  • isEmpty β€” tell you if the stack has nothing in it.
  • size β€” tell you how many items are inside.

That is the whole set. Every stack, in every language, is built from these. The big idea is LIFO, which means Last In First Out. The last item you pushed is the first item you pop.

Note

A stack never lets you reach into the middle. If you want the third plate from the top, you must pop the two plates above it first. That rule is what keeps a stack predictable.

πŸ“₯ push β€” Add to the Top

The push operation takes one item and places it on top of the stack. Nothing below it moves. The new item simply becomes the new top.

Steps to push an item:

  1. Take the item you want to add.
  2. Place it on top of the stack.
  3. The top now points to this new item.
  4. The size goes up by one.

So if your stack has 10, 20 and you push 30, the stack becomes 10, 20, 30 with 30 on top.

πŸ“€ pop β€” Remove from the Top

The pop operation removes the top item and returns it. The item just below becomes the new top. The one case to be careful about is an empty stack. If there is nothing to remove, popping is an error. So you always check first.

Steps to pop an item:

  1. Check if the stack is empty. If yes, stop. There is nothing to remove.
  2. Read the top item.
  3. Remove it from the stack.
  4. The size goes down by one.
  5. Return the removed item.

Caution

Popping from an empty stack is the most common stack bug. Different languages react differently. Some throw an error. Some return a special empty value. Always call isEmpty before you pop when you are not sure.

πŸ‘€ peek β€” Look Without Removing

Sometimes you only want to see the top item. You do not want to remove it. That is what peek is for. It returns the top item but leaves the stack exactly as it was.

Steps to peek:

  1. Check if the stack is empty. If yes, there is nothing to look at.
  2. Read the top item.
  3. Return it. Do not remove anything.

Think of peek as glancing at the top plate without lifting it off the pile.

❓ isEmpty and πŸ“ size

These two are small but you use them all the time.

  • isEmpty returns true when the stack has no items, and false when it has at least one. You call it before pop and peek to stay safe.
  • size returns the count of items currently in the stack. Handy when you need to know how much you are holding.

πŸ’» All Operations in Code

Now let us see every operation working together. The code below pushes a few numbers, peeks at the top, checks the size, then pops items one by one and prints the stack each time. Each language uses its own built-in stack so you see the real, everyday way to do it.

In Python you just use a normal list as a stack. append is push, pop removes and returns the last item, and the last element stack[-1] is the top. It is the simplest of all.

stack_operations.py
# A Python list works as a stack
stack = []
# push: append adds to the end (the top)
stack.append(10)
stack.append(20)
stack.append(30)
# peek: the last element is the top
print("Top item:", stack[-1])
print("Size:", len(stack))
# pop: removes and returns the top
print("Popped:", stack.pop())
print("Popped:", stack.pop())
print("Top item now:", stack[-1])
print("Is empty?", "yes" if len(stack) == 0 else "no")
print("Size:", len(stack))

The output of the above code will be:

Output
Top item: 30
Size: 3
Popped: 30
Popped: 20
Top item now: 10
Is empty? no
Size: 1

Tip

Notice the pattern in the output. We pushed 10, 20, 30. The first pop gave back 30, the last one we pushed. That is LIFO in action. Last in, first out, every single time.

⏱️ How Fast Are These Operations?

This is the best part about a stack. Every core operation is O(1), which means constant time. It does not matter if your stack has ten items or ten million items. push, pop, and peek each touch only the top, so they take the same tiny amount of time.

Operation What it does Time Complexity
push Add an item on top O(1)
pop Remove the top item O(1)
peek Read the top item O(1)
isEmpty Check if the stack is empty O(1)
size Count the items O(1)

Caution

Common mistakes to avoid:

  • Popping or peeking on an empty stack. Always check isEmpty first.
  • Trying to reach an item in the middle. A stack only gives you the top.
  • Forgetting that C++ pop() does not return the item. Read top() before you call pop().
  • Mixing up push and pop direction. push goes on top, pop comes off the top, never the bottom.

🧩 What You’ve Learned

  • βœ… A stack works with a small set of operations: push, pop, peek, isEmpty, and size.
  • βœ… push adds to the top, pop removes from the top, and peek lets you look without removing.
  • βœ… A stack follows LIFO, so the last item you push is the first one you pop.
  • βœ… Always check isEmpty before you pop or peek, so you do not crash on an empty stack.
  • βœ… Every core operation is O(1), meaning it stays fast no matter how big the stack grows.
  • βœ… Each language has its own built-in stack: a list in Python, an array in JavaScript, std::stack in C++, the Stack class in Java, and a simple array with a top index in C.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    Which operation removes the top item from a stack and returns it?

    Why: pop takes the top item off the stack and returns it, lowering the size by one.

  2. 2

    What does peek (also called top) do?

    Why: peek reads and returns the top item but leaves the stack unchanged.

  3. 3

    A stack follows LIFO. If you push 10, 20, 30, which item pops first?

    Why: LIFO means Last In First Out, so 30, the last pushed, pops first.

  4. 4

    What is the time complexity of push, pop, and peek on a stack?

    Why: Each of these touches only the top, so they run in constant time O(1) no matter how big the stack is.

πŸš€ What’s Next?

Share & Connect