In the game, this was a ramen shopPlay “Plates & the Ramen Line”
| In the game | In real IT |
|---|---|
| Bowl | Data |
| Bowl pile (take from the top) | Stack (LIFO) |
| Waiting line (out from the front) | Queue (FIFO) |
| Stack / Take top | push / pop |
| Line up / Take front | enqueue / dequeue |
Function calls are managed with a stack
Say that in a program, function A calls function B, and B calls C. When C finishes, it must return to where B left off; when B finishes, back to A. The place that piles up these “return points” is the call stack. Each call is a push and each finish is a pop, so the last one called finishes first: last in, first out.
If you write a function that calls itself (recursion) and forget the condition that stops it, the calls pile up endlessly, run out of room, and the program crashes. This is a stack overflow — also the name of a famous Q&A site for programmers.
Reverse Polish notation is calculated with a stack
In an everyday expression like “(3 + 4) × 2,” the symbols + and × go between the numbers. In Reverse Polish notation, the symbols go after: “3 4 + 2 ×.” Its strength is that it needs no parentheses and can be calculated left to right with nothing but a stack. There are only two rules:
- When a number comes, push it
- When a symbol comes, pop two, calculate, and push the result
| Read | Stack contents (right is the top) |
|---|---|
| 3 | 3 |
| 4 | 3 4 |
| + | 7 |
| 2 | 7 2 |
| × | 14 |
The 14 left at the end is the answer. For subtraction and division, of the two you pop, the one popped first is the number you subtract or divide by, so watch the order.
A queue is built by turning an array into a “ring”
When you build a queue from an array, the front empties out every time you take something. Shifting everything forward is wasteful, so when you reach the end of the array you wrap around to the start, treating it like a ring. This is a ring buffer. You just move the head and tail positions, so it’s fast, and it’s often used to buffer incoming data in video playback and inside network equipment.
Queues that let some cut in line, and queues that connect systems
- Priority queue: a queue that takes out the highest-priority item first, not the first to arrive. It’s like an emergency room seeing the most serious cases first. Operating systems use it to decide the order of tasks too
- Message queue: in large systems, a queue sits between the part that takes orders and the part that handles stock and shipping. Even if orders flood in at once, the front end just puts them in the queue and replies “received.” The processing side takes them out at its own pace. If one side stalls for a bit, the other keeps running
Monitoring screens often show how many items are waiting in a queue (the queue length). If a queue that normally holds 0 to a few items keeps growing into the hundreds or thousands, it’s a sign the side taking items out has stopped or slowed down. Like a print queue that never shrinks, the cause is usually at the exit, not the entrance.
Practice questions
Four-choice questions in the style of Japan’s Fundamental Information Technology Engineer Examination. Tap below to see the answer.
Q1What is the result of the expression written in Reverse Polish notation as “5 3 − 4 ×”?
- A2
- B8
- C17
- D−7
Show the answer and explanation
Answer: BPush 5 and 3; − pops two and pushes 5 − 3 = 2. Push 4; × gives 2 × 4 = 8. In ordinary notation it’s (5 − 3) × 4.
Q2Which data structure is most appropriate for recording return points and the like when functions are called?
- AQueue
- BStack
- CHash table
- DTree
Show the answer and explanation
Answer: BThe last function called finishes and returns first, so a last-in-first-out stack is used. This is called the call stack.
Q3What is the most appropriate benefit of placing a queue between an order-taking system and a stock-allocation system?
- AOrders are encrypted automatically
- BOrder-taking doesn’t have to stop even when orders suddenly surge, and the processing side can work at its own pace
- CThe order of orders is shuffled randomly
- DThere’s no longer any need to count stock
Show the answer and explanation
Answer: BBecause the queue absorbs orders in between, the order-taking side and the processing side can run without matching each other’s speed. If one stalls for a bit, the other keeps running.
- Function calls use the call stack; pile up too much and you get a stack overflow
- Reverse Polish notation: “push numbers; for a symbol, pop two and calculate”
- A queue is built by making an array into a ring. Placed between systems, it absorbs sudden surges