All episodes · From a List to Your Map App · Episode 2

Arrays vs Linked Lists: Where Your Data Lives

Pressing play loads the video from YouTube.

Put one new number at the front of a list of a million. In an array, a million numbers have to move. In a linked list, you change two arrows. So why do arrays still win most of the time?
We see how an array finds any item in one step (and why a table is stored row after row), what inserting really costs, how a linked list follows arrows through memory, and how the CPU cache makes neighbors fast.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Put one new number at the front of a list of a million. In one kind of list, a million numbers have to move. In the other kind, you change two arrows. Today: the two oldest ways to keep data in memory, and why neither one always wins.

Memory is one long row of numbered cells. We built it in season one, episode eight. An array puts items side by side. Say it starts at address one hundred, and each item takes four bytes. Then item five, counting from zero, is at one hundred, plus five times four: one hundred twenty. One multiply, one add, and you're there. Any item, one step. Tables work the same way. A grid is stored row after row, so row two, column three is just item eleven: one more address.

The catch is inserting. To put a new number at the front, every item has to shift over one place to make room. Eight items, eight moves. A million items, a million moves. And when the array is full, you need a bigger block somewhere else, and you copy everything over. That's why many languages grab double the space each time.

A linked list does it differently. Each item sits wherever there's free space, and carries an arrow: the address of the next item. To insert at the front, point the new item at the old first one, then point the start at the new one. Two arrows, and nothing moves. Removing works the same way: point around the item, and it's gone. But finding the fifth item means starting at the front and following arrows: one, two, three, four, five. A million items, up to a million hops.

So which is faster in real life? Remember the cache from season one, episode fourteen? When the CPU reads one cell, it brings the whole neighborhood along: a cache line. An array's items are neighbors, so the next few are already there. A linked list's items are scattered. Each hop can be another slow trip out to main memory. That's why arrays win most everyday jobs. Linked lists earn their place when items keep being added and removed, and you already know where.

Arrays: jump to any item, but inserting means moving. Linked lists: insert anywhere, but finding means walking. Next time: two simple rules built on top of these, behind every undo button and every waiting line. Stacks and queues. See you there.

Next episode: stacks and queues, the two simple rules behind every undo button and every waiting line.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal