⤠Like
š Save
š Share

Eric Hu
DSA Stacks & Queues
š Stacks & Queues Cheatsheet
Stacks are LIFO, queues are FIFO. This cheatsheet covers both structures, their operations, and the classic problems they solve.
ā Stack operations ā Queue operations ā Monotonic stack ā Deque ā Classic problems
#dsa #stack #queue #deque #interview #coding #tips
Stacks and queues are linear structures with restricted access. Stacks are last-in-first-out (LIFO), queues are first-in-first-out (FIFO).
Stack
Last in, first out. Like a stack of plates.
js
const stack = [];
stack.push(1); // add to top
stack.push(2);
stack.push(3);
stack.at(-1); // 3 ā peek at top
stack.pop(); // 3 ā remove from topAll operations are O(1).
Queue
First in, first out. Like a line at a store.
js
const queue = [];
queue.push(1); // enqueue at back
queue.push(2);
queue.push(3);
queue[0]; // 1 -> peek at front
queue.shift(); // 1 -> dequeue from frontā ļø shift() on an array is O(n). For performance-critical code, use a real queue with a head pointer.
js
class Queue {
#items = [];
#head = 0;
enqueue(item) {
this.#items.push(item);
}
dequeue() {
return this.#items[this.#head++];
}
get size() {
return this.#items.length - this.#head;
}
}Full-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.
www.thedevspace.io
