seegongsik
Data

What is a stack and a queue

If you have stacked plates one on top of another, which do you take first? The one on top, the plate you put there last. If people are standing in line, who goes in first? The one at the front, who came first. Handling data has these same two ways: stacking is called a stack, and lining up is called a queue.

01

Stacking like plates: the stack

In the last lesson we saw how to lay data out in a row. This time, instead of laying it out, let's pile it up.
Picture a stack of plates. A new plate always goes on top, and you take plates off the top as well. So the plate you put on last is the first one your hand reaches.

A
B ← top
Stack plates and take from the top

Tap to stack a plate on top, then tap take out. The top one, the last you put on, always comes out first.

Each time you took one out, the one you had just put on came out first. What goes in last comes out first: that is the promise of a stack.
There is only one place to put things in and take them out, the top, so the rule is very simple. Putting in is called push, and taking out is called pop. We will see where such a simple rule is useful a little later.

02

Lining up: the queue

This time, instead of stacking, let's line things up side by side. Picture a line waiting for a bus. A newcomer stands at the back, and when the bus comes, people board from the front.
So whoever came first goes in first, the exact opposite of stacking. A line that you join at the back and leave from the front like this is called a queue.

frontback
1
2
Add at the back, let out from the front

Tap to add a person to the back of the line, then tap let out. The one at the front, who came first, always leaves first.

Each time you let someone out, the person who had waited longest left first. What goes in first comes out first: that is the promise of a queue.
People join at the back and leave at the front, so the order of arrival is kept exactly. That guarantees fair turns with no cutting in line. In a stack the last to arrive goes first; in a queue the first to arrive goes first.

03

A stack is for undoing

When you make a mistake while writing and press undo, the thing you just did is cancelled first, and what you did before that is cancelled next.
The program stacks up your actions in order, then takes them off the top, starting with the last one, and cancels them one by one. That is a stack. A browser's back button works the same way by stacking the pages you have visited, and when a program calls functions, the computer stacks up the calls in order too.

Stack actions, then press undo
Stack actions, then press undo

Tap to stack up actions, then tap undo. The last action you did is cancelled first, then the rest in order.

Each time you pressed undo, the most recent action disappeared first. When an order has to be rewound backward, nothing fits as well as a stack, because taking out the last thing put in first is exactly what rewinding means.
So what should you use when the order must be kept just as things arrived, not reversed?

04

A queue is for waiting lines

What happens when several people send documents to one printer? The document sent first is printed first, and later ones wait behind it. This is a print queue.
A numbered ticket at a bank works the same way: the number drawn first is called first. Handling things fairly in the order they arrived is exactly what a queue does.

print queue
doc-11
doc-22
Send docs; the one sent first prints first

Tap to add a document to the print queue, then tap print. The one sent first is handled first, then the rest in order.

Each time you pressed print, the document sent earliest came out. When an order must be kept as it is, nothing beats a queue, because taking out the first thing put in first is what fairness means here.
With the same data, one rule about taking things out changes the use this much. Now let's set the two side by side and sum up.

05

Summary

In short: a stack is stacking plates, so the last thing in comes out first. A queue is lining up, so the first thing in comes out first.
Undo and the back button are stacks; waiting lines and numbered tickets are queues. A single rule about putting in and taking out decides the use. Just remember: in a stack the last one goes first, in a queue the first one goes first.

1. stack = stacking plates (last-first)
2. queue = lining up (first-first)
3. stack for undo and back
4. queue for waiting lines and tickets
Press the button to review the key points in order

Tap the key points in order to review. (stacking: stack → lining up: queue → stack for undo → queue for waiting lines)

Now you know two ways to handle data: stacking it and lining it up. Simply deciding where things go in and come out turned the same data into completely different tools.
So far we have looked at containers that hold data inside a program. The next lesson introduces the database, a huge warehouse that keeps very large amounts of data for a long time, so it does not vanish when a program is switched off.

In one lineA stack is stacking plates. You put things on top, and when you take one out, the top one (the last one you put in) comes out. So what went in last comes out first. A queue is lining up. You join at the back, and when you take one out, the front one (the one that came first) comes out. So what went in first comes out first. When you want to undo what you just did or go back, a stack fits perfectly, because the last action is cancelled first, then the rest in order. When you want to handle things fairly in the order they arrived, like a print queue or a numbered ticket, a queue fits. Just remember: in a stack the last one goes first, in a queue the first one goes first.
← Data
Was this helpful? Support seegongsik