Two restricted lists that power everything from undo history to breadth-first search: last-in-first-out and first-in-first-out.
A stack only adds and removes at one end (push/pop, LIFO) β it's how function calls, undo, and depth-first search work. A queue adds at the back and removes at the front (enqueue/dequeue, FIFO) β it's how task scheduling and breadth-first search work. Both give O(1) operations; the entire difference is which end you take from.
Step through stacks & queues live, one move at a time β this becomes a fully interactive visualizer.
Coming soonpush(x) adds to the top, pop() removes from the top β the most recent item leaves first (LIFO).enqueue(x) adds to the back, dequeue() removes from the front β the oldest item leaves first (FIFO).function isBalanced(s) {
const stack = [];
const pairs = { ")": "(", "]": "[", "}": "{" };
for (const ch of s) {
if ("([{".includes(ch)) stack.push(ch);
else if (ch in pairs) {
if (stack.pop() !== pairs[ch]) return false;
}
}
return stack.length === 0;
}
isBalanced("{[()]}"); // true
isBalanced("{[(])}"); // falseThe language runtime keeps a call stack of active function frames β recursion *is* a stack. Any recursive algorithm can be rewritten iteratively by managing an explicit stack yourself.
Removing an array's first element shifts every remaining element left, O(n) per dequeue. Use a head index into the array, a linked list, or a circular buffer to make dequeues O(1).
In name only: it releases the highest-priority item rather than the oldest. It's implemented with a heap, not a FIFO list β see the Heaps & Priority Queues topic.
Share what clicked for you and see notes from other learners on this topic.
Coming soon