Module 01 · DSA · The toolbox · about 17 min to read
Continue where you left off ×
Linked Lists, Stacks & Queues
Linked Lists Stacks and Queues · Open board
It draws three ways of lining things up, and this lesson tells the story of where each rule for “what goes first” came from. Builds on Arrays & Strings and ties into How Code Becomes Execution (compilers, stack & heap) (the call stack) and The Memory Hierarchy (cache, locality, the memory wall) (why arrays usually beat linked lists).
Before you start. You need one fact from Arrays & Strings : an array keeps its values side by side in memory (why that is the fast kind of memory: The Memory Hierarchy (cache, locality, the memory wall) ), so slotting a new one into the middle means sliding every value after it down a slot (Arrays & Strings ). Everything below is what people invented to dodge that slide, plus two rules for which item you’re allowed to touch first.
The idea in 30 seconds
An array’s insert-in-the-middle problem is what the chain of nodes was invented to dodge: give every value a pointer to the next one, and inserting only ever touches two nodes.
The chain is fast to splice and slow to search, the opposite of an array, which is why arrays usually win in practice.
A stack is that same chain with one rule: touch only the top, last in first out. Every function call you make runs on one.
A queue is the same chain with the opposite rule: touch only the front, first in first out. Built the obvious way it’s slow. A ring buffer, two markers instead of one sliding row, fixes it.
Read the story
Lesson 5 of 96 in DSA
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson The problem: a pile of facts that won’t hold still January 1956: the pointer, a card that says where the next one is 1955, Munich: the stack, a pile with one rule The queue: the same trail, a fair rule, and the ring buffer that fixes it Terms it explains: node, pointer, stack, queue, ring buffer, Singly linked, doubly linked, deque, monotonic deque, 3, 10, ADT, call stack, head/tail, interface vs implementation, LIFO/FIFO, linked list, priority queue, ring / circular buffer, singly / doubly linked.
Review DSA
Mark as read Saved in this browser, and to your account when you sign in.