Mr. Tono’s seed trays pile on the stand (a stack: push and pop) and the watering cans queue at the well (a queue: push and shift): every step of the real…
Why it matters
The base of a lot: undo, the call stack and DFS use a stack; task queues, BFS and the event loop use a queue. And one performance trap: shift() moves every element, so a big queue on shift() gets slower and slower.
The key idea
A stack and a queue differ in one thing: which end comes out
Common mistakes
array.shift() to dequeue By the spec, shift() moves every remaining element: O(n) per call. For a big queue use a kepala index, two stacks, or a linked list.
pop from an empty stack In JavaScript pop() on an empty array gives undefined, not an error. Check .length first when it matters.
Thinking a stack and a queue are different structures Both are lists; only the rule for which end comes out differs. A JS array can be either.
Try it in this playground
Sow, then take The tray sown last comes off first (LIFO).
Fill a can The can that came first is filled first (FIFO).
Count what shift() moves Fill a can from a line of at least four: see how many step up.
Use a head index Fill a can with antre[kepala++]: zero moves.