All chapters[•]Data StructuresChapter 02
Data Structures · Chapter 2

Arrays and Linked ListsData Structures · Chapter 2

Where you put things in memory decides which operations are cheap. An array keeps its items side by side, so item i is one multiplication away, but inserting in the middle shifts everything after it. A linked list keeps its nodes anywhere and joins them with references, so inserting at the front moves nothing, but reaching the middle means walking. This chapter builds both, measures what each operation really costs, shows how a dynamic array grows without ever running out of room, and teaches the habit that decides whether pointer code is right: the order of the updates.

Instructors: Boxi Yu · Yintong Huo · Python and Java versions (switch at the top) · Every program here has been run, and every output shown is the real output. · All chapters

Array or linked list? Count the work

Two rows with the same n items: an array and a linked list. Choose "read item i", "insert at the front", "insert in the middle" or "remove the last item". Counters show the item moves (array) and the reference hops and changes (linked list) for each, and the counts grow with n as the chapter predicts.

Why this matters

about 100–130 minutes (4 videos of 8–12 minutes plus the exercises).

A layout decides the costs

"Side by side" gives constant-time access by index and expensive insertion in the middle; "joined by references" gives the opposite. Every structure later in the course is one of these two ideas, or a mix.

Growth is a design problem

A fixed block cannot grow, so a dynamic array allocates a bigger one and copies. Doubling the size makes that cheap on average; growing by a constant makes it quadratic. The same amortized argument returns in hashing and heaps.

Pointer code is easy to get almost right

Update the references in the wrong order, or forget the tail, and a list silently loses nodes. The same bug is common in code written by an AI, and a structural check (size equals the number of reachable nodes) finds it.

5 parts

Four videos with their notes, then the summary and practice. Read them in order.

01

Arrays in memory

An array stores its items side by side in one block of memory, every item the same size, so item i sits at base + i × size: one multiplication and one addition, however long the array is. The price is a fixed capacity, and inserting or removing in the middle shifts every item after the position. We count the shifts and then measure them.

address formulacapacity and sizeinsert and remove shiftO(1) access, O(n) insertion
Open →
02

Dynamic arrays and resizing

A fixed block cannot grow, yet programs append forever. A dynamic array reserves a bigger block, copies every item, and abandons the old one. If the new capacity is twice the old one, the copying adds up to fewer than 2*n* item copies over n appends, so an append is O(1) amortized. Growing by a fixed amount instead makes the total quadratic.

reservecopydiscarddoublingamortized O(1)growth factor
Open →
03

Linked lists: rewiring pointers

A linked list is nodes, each holding a value and a reference to the next node; the list itself is just a reference to the first one, head. Inserting or removing at the front, or inserting after a node you already hold, changes one or two references and moves no item. The order of the changes matters: do it the wrong way round and the rest of the list becomes unreachable. And finding the middle node still takes a walk.

nodeheadreferenceadd_firstinsert_afterorder of updateshops
Open →
04

Singly, doubly and circularly linked lists

We can add and remove at the front of a linked list, and add at the end with a tail reference, in constant time. Removing the last node is different: the node before it is needed, and a singly linked node does not know its predecessor. A doubly linked list gives every node a prev reference, two sentinel nodes remove the special cases, and a circular list makes the last node point back to the first.

prev referencedelete_nodesentinelscircular listrotateextra reference per node
Open →
05

Summary and practice

A one-page map of the chapter, five self-check questions, ten exercises with solutions, and a way to test claims an AI makes about complexity.

self-checkexercises
Open →

How to use these notes

Watch · Predict · Step through · Practise

STEP 1

Watch

Each part starts with its video, 8–10 minutes, in Python or Java.

STEP 2

Predict

Answer the amber Predict box before you open the answer.

STEP 3

Step through

The interactive demos are being added; each one is described where it will appear.

STEP 4

Practise

Finish with the self-check and the exercises, with templates in your language.