๐Ÿงฎ
๐Ÿงฎ Data Structures & Algorithms ยท DS-101 ยท Session 4 of 6

Stacks, Queues & Beyond

Some structures aren't about storage but about *order of access* โ€” stacks (last-in-first-out) and queues (first-in-first-out) model how work gets done, and show up everywhere.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

Not every structure is about how you store data - some are about the order in which you take it out. Two classics: the stack (last-in, first-out, like a pile of plates) and the queue (first-in, first-out, like a line of people). They sound simple, but this ordering idea underlies undo systems, task scheduling, browser history, and much more.

Stacks and queues: order of access

The difference is which item you take next: a stack gives you the most recently added; a queue gives you the earliest added.

StructureRuleEveryday example
Stack (LIFO)Last in, first outUndo, browser back, a pile of plates
Queue (FIFO)First in, first outA line, a print queue, task scheduling
DequeAdd/remove at both endsA history you cap at both ends

These aren't storage tricks - they're access disciplines. When you need 'most recent first', that's a stack; when you need 'fair, in order', that's a queue. Recognising the pattern is the skill.

They're everywhere

Once you know stacks and queues, you start seeing them in real systems - because they match how work naturally flows.

๐ŸŽฏ
Undo is a stack; a to-do line is a queue. Undo works because each action is pushed onto a stack and popped off in reverse - last action undone first. A print queue or task list processed fairly is a queue - first submitted, first done. Even running your code uses a 'call stack' (the most recent function returns first). Spotting whether a problem wants last-in-first-out or first-in-first-out tells you which structure to use, and often clarifies the whole solution.

Use the right access order

  • Stack (LIFO) - most recent first: undo, back-navigation, reversing.
  • Queue (FIFO) - earliest first: fair processing, scheduling, buffering.
  • Deque - efficient add/remove at both ends (great as a capped history).
  • Recognise the pattern - which end do you take from? - to pick the structure.

๐ŸŽฏ Key takeaways

  • Stacks and queues are about the order you take items out, not how you store them.
  • Stack = last-in-first-out (undo, back button, call stack); Queue = first-in-first-out (a line, scheduling).
  • A deque adds/removes efficiently at both ends - great for capped histories and buffers.
  • Recognising LIFO vs FIFO in a problem often clarifies the whole solution.

Frequently asked

How do I know whether a problem needs a stack or a queue?

Ask which item you need to handle next: the most recently added, or the earliest added. If you want the most recent first - undoing the last action, backtracking, reversing something, processing nested structure - that's last-in-first-out, a stack. If you want to handle things in the order they arrived, fairly - a waiting line, tasks to process in submission order, buffering a stream - that's first-in-first-out, a queue. A quick test: 'undo' and 'go back' are stacks; 'wait your turn' and 'process in order' are queues. Once you spot the LIFO-vs-FIFO nature of the problem, the right structure - and often the whole approach - becomes clear.

Can't I just use a list for stacks and queues?

For a stack, yes - a Python list works perfectly: append() to push and pop() to remove from the end are both fast, so a list IS a good stack. For a queue, a plain list is a poor choice, because removing from the front (to get the first-in item) requires shifting every other element, which is slow. That's exactly what a deque (double-ended queue) is for - it adds and removes efficiently at both ends, making it the right tool for queues. So: a list is a fine stack, but use a deque for a queue. The concepts (LIFO/FIFO) are what matter; the implementation is just choosing a structure whose fast operations match the access pattern you need.

Session 4 of 6 complete

Next up: Trees & Graphs: Modelling Relationships

Continue to Session 5 โ†’

Related lessons

More in this course

Course home → · Browse all courses →