Before learning a new data structure, it helps to first understand the *problem* it solves. So let's look at some real situations that programmers face when building software. These examples will show us why we sometimes need a special way to store and process data.
Many times in a program, we need a place to keep data that is smart enough to remember the order in which the data was added. Often, we want to take the data back out in the *reverse* order — the most recent item first, and the oldest item last.
This pattern has a name:
Both names describe the *same* idea: the item added last is the first one to come out.
A simple way to picture this is a pile of plates. You add new plates on top, and when you need a plate, you take the one from the top — the last one you put down. You don't pull a plate from the bottom of the pile. That is LIFO in everyday life.
This may sound like a small detail, but a surprising number of features in the software you use every day depend on it. Let's look at three of them.
This is probably the example you see most often. Every web browser has a back button that returns you to the page you were just on.
Imagine you visit pages in this order:
To make the back button work, the browser has to store every page you visit. When you press back, it gives you the pages in *reverse* order:
Notice the pattern: the page you visited last (Google) is the first one you return from. That is exactly LIFO. The browser keeps a history pile and pulls pages off the top.
Almost every text editor has an undo feature. It lets you reverse your changes, one step at a time, in the order you made them.
Suppose you type three lines, one after another:
This is some textThis is some more textThis is even more textNow you press undo:
This is even more text (your most recent change)This is some more textAgain, the change made last is the first one to be undone. To do this, the editor stores each change and retrieves them in reverse (LIFO) order. Same idea as the browser, different feature.
This last example is happening inside the computer itself, even if you never see it.
When one function calls another function, the program must remember where to go back to once the called function finishes. When a function ends, it returns control to the function that called it. That function eventually finishes and returns to *its* caller, and the chain continues back up.
Look at this code:
funcA() {
print("funcA")
}
funcB() {
funcA();
}
funcC() {
funcB();
}
Call funcC()
Here is the call order (the order functions start running):
funcC() is calledfuncC calls funcB()funcB calls funcA()Now here is the return order (the order functions finish) — and it is the exact reverse:
funcA finishes first and returns to funcBfuncB finishes and returns to funcCfuncC finishes lastSo funcA — the last function to start — is the first to finish. That's LIFO once more. The processor in your computer uses a special data structure to remember this chain of calls and return control in the correct reverse order. (You may have heard the phrase "call stack" — this is exactly why it works.)
The browser back button, the editor undo, and nested function calls all share one need:
> Store items, then take them back out in reverse order — last in, first out.
When the same pattern shows up again and again across very different problems, it usually means there is one clean tool designed to handle it. In this course, we will learn how all of these problems can be solved with a simple but powerful data structure called the stack.