Workstation topics · Data structures in plain words
Queues and stacks: who goes next?
🏠The everyday picture
At a railway ticket counter, the first person to join the queue is the first served. In a canteen, clean plates are put on top of a stack, and the next person takes the top plate: the last one put down is the first one used. Two simple rules, and programs use both all the time.
📘What it really is
-
A queue is first in, first out (FIFO). You add at the back and take from the front. Used for anything that must be handled in arrival order: print jobs, customer support tickets, orders waiting to be packed, messages waiting to be sent.
-
A stack is last in, first out (LIFO). You add to the top and take from the top. Used where the most recent thing matters first: Undo (Ctrl+Z takes back the latest change first), and the steps a program is in the middle of.
Both keep things fair and predictable: nobody jumps the queue, and Undo always takes back the latest change first.
📝A worked example
Sharma Textiles packs online orders in the order they're paid:
queue: [order 101, order 102, order 103]
new order 104 arrives → added at the back: [101, 102, 103, 104]
packer takes next → 101 comes off the front: [102, 103, 104]
Priya types three changes in a document: adds a title, makes it bold, changes the colour. She presses Undo:
stack of changes: [title, bold, colour] ← colour is on top
Undo → takes "colour" off the top: the colour change is undone first
Undo → takes "bold" off next
🖱️Try it
Look around today for one queue and one stack (a bus queue, a pile of letters on a desk, a stack of chapatis). For each, ask: is the newest one handled first, or the oldest? Then think of one app feature that works the same way.
✅Quick check
Customers call a helpline and wait for the next free person. Queue or stack?
Show the answer
A queue. The person who has waited longest should be answered first: first in, first out.
You press Undo three times. Which change is undone first?
Show the answer
The most recent one. Undo works like a stack: last in, first out.
🧩Check yourself
Tap an answer to see why every option is right or wrong. There's no score and nothing is saved, so try as often as you like.
