Stacks: last in, first out
After this: Say what a stack is, and keep one in an array with a top index.
On this page · 8 sections · 1 check
Press undo. The last thing you did is the first thing undone. Press it again, and the thing before that is undone.
Now picture a pile of plates. You put each clean plate on top of the pile. When you need a plate, you take the top one. You do not pull a plate out of the middle.
Undo and the pile of plates follow the same rule, and so does the back button in a web browser: the last thing to go on is the first thing to come off. This lesson turns that rule into a tool you can use in a program.
What a stack is
A is a pile of values that follows this rule. You can do two things with it.
- Push puts a value on top.
- Pop takes the value off the top and gives it back to you.
Here is a stack of plates, one step at a time.
- Push a red plate. The pile holds red.
- Push a blue plate. The pile holds red, with blue on top.
- Push a green plate. The pile holds red, then blue, with green on top.
- Pop. You get green, the last plate you added.
- Pop. You get blue. Only red is left.
The plates come off in the opposite order to the way they went on.
Last in, first out
This rule has a name: last in, first out. You will often see it shortened to . When someone says a stack is LIFO, they mean what you saw with the plates.
Why anyone wants a stack
Think about undo in a text editor. Each time you make a change, the editor pushes a note of that change onto a stack. Suppose you type a word, make it bold, then delete a line. The stack now holds those three changes, with "delete a line" on top.
- Press undo. The editor pops "delete a line" and puts the line back.
- Press undo again. The editor pops "make it bold" and removes the bold.
The editor never has to search for the right change, because the one it wants is always on top. That is why the strict rule is useful. You can only reach the top, but the top is the only thing these programs need.
Keeping a stack in an array
A program has no pile of plates, so it needs somewhere to keep the values. An array works well. It is a row of cells, and each cell has an index, which is its position number.
We add one extra number, called top. It holds the index of the next free cell.
Take an array of four cells, with top at 0. Now push 7, push 9, then pop. In the table, _ marks an empty cell.
| Step | Cells | top | What happened |
|---|---|---|---|
| Start | [_, _, _, _] | 0 | The stack is empty. |
| Push 7 | [7, _, _, _] | 1 | 7 goes in index 0, then top goes up by one. |
| Push 9 | [7, 9, _, _] | 2 | 9 goes in index 1, then top goes up by one. |
| Pop | [7, _, _, _] | 1 | top goes down to 1. Index 1 holds 9, so pop gives back 9 and clears the cell. |
So the two rules are these.
- To push, write the value at
top, then add one totop. - To pop, subtract one from
top, read that cell, then clear it.
Pop subtracts first because top names the next free cell. The newest value sits one cell below it.
Notice that top also tells you how many values the stack holds. It was 0 at the start, then 1, then 2, then 1 again.
The same steps in code
Here are those steps as plain lines of code.
cells = [None] * 4 # a row of four empty cellstop = 0 # the next free cell
cells[top] = 7 # push 7top += 1
cells[top] = 9 # push 9top += 1
top -= 1 # popvalue = cells[top]cells[top] = Noneprint(value, top, cells)9 1 [7, None, None, None]
int[] cells = new int[4]; // a row of four cells, 0 means empty hereint top = 0; // the next free cell
cells[top] = 7; // push 7top++;
cells[top] = 9; // push 9top++;
top--; // popint value = cells[top];cells[top] = 0;System.out.println(value + " " + top + " " + Arrays.toString(cells));9 1 [7, 0, 0, 0]
- The first two lines create the empty row and set
topto 0. Here, None0 stands for an empty cell. - Each push is two lines: write at
top, then add one. - The pop is three lines: subtract one, read the value, clear the cell.
- The output shows that pop gave back 9, that
topis 1, and that only 7 is left.
Watch it happen
Push three values, pop one, push one more, then pop again. Watch which cell each step touches and where top sits.
- Push 1Push 1. It goes in index 0, the cell that top names. Then top goes up to 1.
- Push 2
- Push 3
- Pop
- Push 4
- Pop
- 3·
- 2·
- 1·
- 0·
Run an operation to begin.
The pops gave back 3, then 4. Each push and each pop touched exactly one cell, and no value moved.
Check yourself
A stack starts empty. Push 1, push 2, push 3, pop, push 4, pop, pop. What do the three pops return, in order?
What comes next
You can now push and pop by hand. The next lesson puts these steps into a class and asks how fast they are.
- Say what a stack is: a pile of values where the last value in is the first value out.
- Keep a stack in an array with a top index: push writes at top and adds one, pop subtracts one and reads that cell.
- Say what a run of pushes and pops gives back, in order.