What a Data Structure Is, and How Memory Works
Memory is a long row of numbered boxes, and opening any box is equally quick. A data structure is an agreed way of arranging things in those boxes, plus a few small notes. See with six songs on a phone why every structure is quick at some things and slow at others.
What you will learn
- Picture memory as a row of numbered boxes that are all equally quick to open
- Explain why one sum reaches any item in a gap-free row
- Explain why adding at the front of a row means moving everything
- Describe a data structure as an arrangement of boxes plus small notes
Before you start
- No coding needed
Before you learn a single data structure, it helps to know what memory really is. The idea is simple. Memory is a long row of numbered boxes, and opening any box is equally quick. A data structure is just an agreed way of arranging things in those boxes, plus a few small notes.
That one idea explains why every structure you'll meet is quick at some things and slow at others. No code needed here; just pictures.
Six songs on a phone
Maya has a phone with six songs: Rain, Moon, Fire, Sea, Wind and Snow. She taps song number five, and Wind plays at once. No waiting.
So how did the phone find Wind so fast? Did it look through songs one to four first? It didn't, and the reason comes from how memory is built.
A wall of numbered lockers
Imagine a long wall of lockers, each with a number on the door: 200, 201, 202, and so on. A locker holds one thing only. Rain goes in 200, Moon in 201, Fire in 202, Sea in 203, Wind in 204 and Snow in 205. One song per locker, with no empty locker in between.
The phone remembers just two things: where the songs start (200) and how many there are (6). When Maya taps song 5, the phone skips the 4 songs in front of it. 200 plus 4 is 204, and locker 204 opens. Nothing was searched. A computer never walks along the wall; it goes straight to the number.
Whichever song she picks, it's one sum and one box. That's why reaching an item by its position in a gap-free row costs the same, however long the row gets.
Adding at the front
Now see where this breaks. Maya wants to add a new song, Star, right at the front. Star needs box 200, so every song has to shift one box along.
And you start at the far end. If Rain moved first, it would land on top of Moon and wipe it out. So Snow goes from 205 to 206, then Wind from 204 to 205, then Sea, Fire, Moon and Rain each move one box along. Six songs have moved. Now box 200 is free, Star goes in, and the notes say 7 songs.
If one more song arrives, it needs box 207. But that box belongs to another app. So all 7 songs have to be copied into a free run of 8 boxes. No gaps is why the sum works, and it's also why this hurts.
Same boxes, new arrangement
So what does a data structure add? Put the same six songs into boxes scattered all over memory: Rain in 317, Moon in 142, Fire in 508, Sea in 263, Wind in 91 and Snow in 430. Each song now carries a small note with the number of the next box.
To reach the fifth song you open 317, then 142, then 508, then 263, and finally 91. That's 5 boxes opened, not one. But adding Star at the front is now easy. Star gets any free box with a note pointing to 317, and the start note changes. Two changes, and not one song moved.
The boxes never changed. Only the arrangement and the notes did.
What happens when the list grows
| Job | Numbered row, 6 songs | Numbered row, a million | Chain, 6 songs | Chain, a million |
|---|---|---|---|---|
| Reach the last song | 1 sum | 1 sum | 6 boxes | a million boxes |
| Add at the front | move 6 songs | move a million | 2 changes | 2 changes |
In plain words: in the row, reaching any song is one sum and one box, but adding at the front moves every song. In the chain, reaching the last song means following the notes through every box, but adding at the front is one new box and a changed start note. No structure wins at everything.
Later lessons write these costs in Big-O notation: one sum is O(1), and work that grows with the number of songs is O(n). The Big-O lesson covers that.
Where this goes next
The rest of the course follows four groups. First, things in a line: rows, chains, stacks and queues (think undo and print jobs). Second, finding fast: hash tables, sorted trees and heaps (think contacts and database lookups). Third, connected things: graphs and shortest paths (the route on a map). Fourth, methods: searching, sorting and smart ways to solve problems.
Each time, ask the same three questions. How is it arranged, what's quick, and what's slow?
Recap
- Memory is a row of numbered boxes, and one sum takes you straight to one box.
- No gaps is why the sum works, and it's also why adding at the front means moving everything.
- A data structure is an arrangement of boxes plus small notes, and that decides what's quick and what's slow.
Next, see the row and the chain as real structures: arrays and dynamic arrays, then linked lists.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
Data Structures and Algorithms Concepts: No Coding Needed
How stacks, trees, hash tables, sorting and graphs really work, step by step, with pictures.View on Udemy ↗: Data Structures and Algorithms Concepts: No Coding Needed (paid course, opens in a new tab)Related posts
Arrays and Dynamic Arrays: Full? Get a Box Twice the Size
A fixed array never gets longer. A dynamic array fills its box, then copies everything into a box twice the size. Build one for Emma's playlist in Python, count the copies and shifts, and see why adding at the end stays cheap while inserting at the front costs almost 50 million shifts for 10,000 songs.
Linked Lists: A Chain of Clues, Built in Python
In a linked list every node holds a value and the address of the next node. Build a treasure hunt chain in Python: a new first clue is two moves at any size, slipping a clue in or out is just changing next, but reaching clue number i is a walk of i hops.
Stacks: Last In, First Out, With the Undo Button
A stack is last in, first out: the newest thing you put on is the first thing you take off. Build Emma's undo button in Python from a list and one top index, push, pop and peek in one step each, undo after a million edits, and see what happens when you pop an empty stack.
Queues: First In, First Out, With a Ring of Cells
A queue is first in, first out: whoever joins the line first is served first. Build Kate's cinema ticket line in Python as a ring of four cells, where nobody steps forward and only head and tail move, serve a million people with the same four cells, then break it with a line that is already full.
