Introduction to Stack
Table of Contents + β
Say you are typing in an editor and you make a mistake. You press Undo. The last thing you did goes away first. Press it again and the thing before that goes away. Notice the order? The most recent action is the first one to be removed. That is exactly how a stack works. That is what we will learn today.
π½οΈ A Real-World Picture
Think about a pile of plates in your kitchen. You wash a plate and put it on top of the pile. You wash another and put it on top again. Now someone needs a plate. Which one do they take? The one on top. The plate you put last is the one that comes off first.
That simple rule has a name. We call it LIFO, which means Last-In-First-Out. The last item you put in is the first item you take out.
You see this everywhere:
- A pile of plates. The last plate placed is the first one used.
- The Back button in your browser. The last page you visited is the first one you go back to.
- A pile of books on your desk. You pick from the top.
There is one catch. You can only touch the top. You cannot pull a plate from the middle without removing the ones above it. A stack follows the same idea.
π What Is a Stack?
A stack is a way to store items where you add and remove from one end only. We call that end the top.
So everything happens at the top. You add to the top. You remove from the top. You look at the top. You never reach into the middle or the bottom directly.
Note
A stack is not a new kind of memory. It is just a rule about the order you add and remove things. The last one in is the first one out. That is the whole idea.
π§± Picture the Stack
Let us add three items one by one: 10, then 20, then 30. Each new item sits on top of the previous one.
So 10 went in first and sits at the bottom. 30 went in last and sits on top. When we remove, 30 leaves first because it is on top. Then 20. Then 10 leaves last. First in, last out.
π§ What a Stack Can Do
A stack keeps its job small. There are only a few actions you ever do with it. Here they are in plain words.
- push β add 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 β check if the stack has nothing in it.
Let us walk through each one.
push means put something on top. Imagine placing a fresh plate on the pile. The stack grows by one and the new item becomes the top.
pop means take the top item off. The plate that was on top is gone now. The one below it becomes the new top. The stack shrinks by one.
peek means just look at the top without taking it. You glance at the top plate to see what it is. Nothing is removed. The stack stays the same size.
isEmpty answers a yes or no question. Is there anything in the stack at all? This matters because you should never pop from an empty stack. There is nothing to take.
Caution
Trying to pop or peek when the stack is empty is a common mistake. Always check isEmpty first. Popping from nothing usually crashes your program or gives an error.
π¬ A Step-by-Step Run
Let us do a small run so the order sinks in. Start with an empty stack and watch the top change.
| Action | Stack after (top on right) | Returned |
|---|---|---|
| push(10) | 10 | β |
| push(20) | 10, 20 | β |
| push(30) | 10, 20, 30 | β |
| peek() | 10, 20, 30 | 30 |
| pop() | 10, 20 | 30 |
| pop() | 10 | 20 |
See how 30 came out before 20? It went in last so it left first. That is LIFO in action.
π» A Tiny Push and Pop Demo
Most languages already give you a ready-made stack. You do not have to build one to start. Here we push three numbers, peek at the top, then pop them and watch the order. Look at how the values come out in reverse of how they went in.
In every language the numbers came out as 30, 20, 10. We pushed 10, 20, 30 but got them back in reverse. That reverse order is the signature of a stack.
π Where Stacks Are Actually Used
This is not just a classroom toy. Stacks run quietly inside many things you use every day.
- Undo and Redo. Each action you do gets pushed on a stack. Press Undo and the last action pops off first. That is why Undo walks backward through your history.
- The function call stack. When your program calls a function, it gets pushed on a stack. When that function finishes, it pops off and control goes back to where it was called. This is the call stack, and your computer manages it for you.
- Browser Back button. Every page you visit is pushed. Back pops the most recent page so you return to where you just were.
- Checking brackets and expressions. Compilers use a stack to make sure every opening bracket has a matching closing one, and to work out math expressions in the right order.
Tip
If a problem talks about βthe most recentβ thing, or βgoing back in reverse orderβ, a stack is very often the right tool. That phrase is your hint.
β οΈ Common Mistakes to Avoid
A few slips trip up beginners. Keep these in mind.
- Popping from an empty stack. Always check isEmpty first or your program may crash.
- Thinking you can grab the bottom item directly. You cannot. You only ever touch the top.
- Mixing up a stack with a queue. A queue is First-In-First-Out, the opposite order. We will cover queues later.
- Forgetting that peek does not remove anything. It only shows you the top.
π§© What Youβve Learned
β A stack stores items and works on one end only, called the top.
β It follows LIFO β the last item in is the first item out, just like a pile of plates.
β The main actions are push (add on top), pop (remove from top), peek (look at top), and isEmpty (check if empty).
β Pushing 10, 20, 30 and popping gives you 30, 20, 10 β reverse order.
β Stacks power Undo/Redo, the function call stack, the browser Back button, and bracket checking.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does LIFO stand for in a stack?
Why: A stack is Last-In-First-Out: the item added most recently is the first one removed.
- 2
You push 5, then 8, then 2 onto an empty stack. What does pop() return first?
Why: 2 was pushed last, so it sits on top and pops off first.
- 3
Which operation looks at the top item without removing it?
Why: peek (also called top) returns the top item but leaves the stack unchanged.
- 4
Why should you call isEmpty before pop?
Why: There is nothing to remove from an empty stack, so popping it usually errors out or crashes the program.
π Whatβs Next?
Now that you know what a stack is, let us go deeper into each operation and then build one from scratch.