Stack and Queue
Last-in-first-out versus first-in-first-out, side by side.
A stack removes the most recently added element (LIFO); a queue removes the oldest (FIFO). Both support push and pop in constant time — the only difference is which end comes out, and that single difference changes everything built on top.
It is why DFS and BFS are the same algorithm with different containers: swap the stack for a queue and depth-first exploration becomes level-by-level. Stacks also drive function calls, undo history and bracket matching; queues drive schedulers and buffers.
Pushing the same four values into both and draining them makes the contrast immediate. AlgoLens draws both containers so you watch one reverse the input while the other preserves it.
The code
#include <bits/stdc++.h>
using namespace std;
int main() {
stack<int> st;
queue<int> q;
for (int x : {1, 2, 3, 4}) {
st.push(x);
q.push(x);
}
while (!st.empty()) {
cout << st.top() << ' ';
st.pop();
}
cout << '\n';
while (!q.empty()) {
cout << q.front() << ' ';
q.pop();
}
cout << '\n';
return 0;
}
Now run your own Stack and Queue
AlgoLens traces your code — arrays, graphs, trees and recursion — from a real execution, so you see how it actually behaves, not a canned animation.
Paste your codeKeep going
Union-Find (DSU)
Merge disjoint sets and query connectivity almost in constant time.
Linked List
Nodes connected by pointers — traversal and insertion made visible.
Priority Queue (Binary Heap)
Always pop the largest — a tree hiding inside an array.
Prefix Sums
Precompute once, then answer any range sum with a single subtraction.