Unit 03

Stacks & Queues

Both restrict how you can add and remove elements, and that restriction is the whole point — it gives you a predictable order of access for free.

Stack (LIFO) top → C B A push/pop only at the top Queue (FIFO) A B C front rear enqueue at rear, dequeue from front

// a stack reverses arrival order, a queue preserves it

What they are

A stack only lets you add or remove from one end — the "top" — so the last thing you added is the first thing you remove (Last In, First Out). A queue lets you add at one end (the rear) and remove from the other (the front), so the first thing added is the first thing removed (First In, First Out).

Why they're useful

Where they struggle

Time complexity

OperationStackQueue
Insert (push / enqueue)O(1)O(1)
Remove (pop / dequeue)O(1)O(1)
Access middle elementNot supportedNot supported

Quick check

← Linked Lists Next: Trees & BSTs →