Lecture notes · CS50: Programming in C · Lecture 5

Data Structures

Harvard CS50 Introduction to Computer Science, lecture 5, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: the course website.

Part 1 · 0:00–10:00

This part introduces data structures, using queues and stacks as examples of ways to organize data in a computer's memory.

Key ideas

Words to know

abstract data type
A description of how a kind of data container behaves, without saying exactly how it is built inside the computer.
queue
A way of holding items where the first one added is the first one taken out, like people standing in line.
FIFO
First in, first out. The first item that goes in is the first item that comes out.
LIFO
Last in, first out. The last item that goes in is the first item that comes out.

Example

Think of a coffee shop line. Customers who join first get served first, which is FIFO and what a queue does. A stack of plates works the other way: you put the newest plate on top and take it off first, which is LIFO.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains why a fixed-size array is hard to grow, and why copying it into a bigger array works but is slow.

Key ideas

Words to know

contiguous
Stored in one unbroken block, with no gaps, back to back.
garbage value
Leftover junk in memory from earlier use. Its contents do not matter, but you still cannot write over it without asking the system first.
malloc
A function that asks the computer for a chunk of memory while the program runs, so the size does not have to be set in advance.
big O of n
A way to say that the amount of work grows in step with the number of items, n.

Example

Imagine three cars parked in a row with no gaps, and a fourth car arrives. If the spot next to them is already taken by something else, you cannot just squeeze the car in. You would need to find a longer empty row, move all the cars over one by one, and then park the new car, which takes time.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how to ask the computer for a block of memory with malloc, treat that block like an array, and resize it by copying the numbers into a bigger block.

Key ideas

Words to know

malloc
A function that asks the computer for a block of memory of a size you choose. It gives you back the address of that block.
NULL
A special address value, 0, that means something went wrong. You should never use memory at this address.
pointer arithmetic
The computer's ability to add or subtract addresses, so that moving from one number in memory to the next is handled for you.
memory leak
Memory that a program got but never gave back. It stays in use and can slow the computer down over time.

Example

Think of renting a storage locker that fits three boxes. When you suddenly need room for four, you rent a bigger locker, move the three boxes over, add the fourth, and give back the old locker so you stop paying for it.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

Instead of storing numbers side by side in one block, this part shows how to link separate chunks of memory together with pointers to form a linked list.

Key ideas

Words to know

pointer
A value that stores the address of a place in memory, so it points to where something else is kept.
node
One box or chunk of memory in a chain. In this lesson, each node holds a value and a pointer to the next node.
linked list
A chain of nodes where each node points to the next one. The nodes can sit in scattered places in memory.
contiguous
Lying next to each other with no gaps, like items placed back to back.

Example

Think of a treasure hunt where each clue tells you where to find the next clue. The clues can be hidden in different rooms of the house, but by following each one you still reach the whole chain. A linked list works the same way: each node tells you where the next node is.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part explains how a linked list works, where each piece of data holds a pointer to the next piece, and how to write one in C.

Key ideas

Words to know

pointer
A variable that holds the memory address of something else, so you know where to find it.
null
A special value, written as 0x0, that means a pointer points at nothing. It is a safe marker for the end of a list.
metadata
Extra information that helps organize your real data but is not the data you care about, such as the pointers in a linked list.
malloc
A C function that asks the computer for a block of memory of a given size and returns its address.

Example

Think of a line of train cars that are hooked together. Each car holds passengers, and each hitch points to the next car. The last car's hitch is empty, which tells you the train ends there, and you can add or remove cars without moving the passengers in the other cars.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part shows how to build a linked list one node at a time, and why the order of updating pointers matters so you don't lose nodes.

Key ideas

Words to know

node
One piece of a linked list. It holds a value (here, a number) and a pointer to the next piece.
pointer
A variable that stores the memory address of something, so you know where to find it.
memory leak
Memory that the program still reserves but can no longer reach, because no pointer points to it anymore.
atoi
A function that converts text (a string of characters) into a whole number. The name stands for ASCII to integer.

Example

Imagine a line of people holding hands, where each person knows who is behind them. If you add a new person at the front, you must first have them hold hands with the current front person, and only then tell the group that they are the new front. If you tell the group about the new front first, the old line gets lost because nobody is holding on to its first person anymore.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part shows how to add nodes to the front or the end of a linked list, how to print every node, and why searching a linked list is slower than searching an array.

Key ideas

Words to know

pointer
A variable that holds the location of something in memory, so you can find it and follow it to the next place.
node
One small piece of a linked list that holds a number and a pointer to the next node.
null
A special value that means nothing is there. At the end of a linked list, the last node's next field is null.
prepend
To add something at the very beginning of a list, before all the existing items.

Example

Think of a treasure hunt where each clue tells you where to find the next clue. You can only reach clue number 3 by first finding clues 1 and 2, and you cannot skip ahead by guessing where the middle clue is. That is how a linked list works: you must follow the chain from the start to reach any node.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:20:00

This part explains how to keep a linked list in sorted order, why that makes adding items slower, and why the lecturer then starts to explore tree structures as a way to get the benefits of both arrays and linked lists.

Key ideas

Words to know

pointer
A stored memory address that tells the program where another piece of data is located.
node
One small container of data, such as a number, that also holds a pointer to the next node.
traverse
To walk through a list one node at a time, starting from the beginning, to reach a place in it.
big O
A way of describing how much longer a task takes as the amount of data grows. Big O of n means the time grows in step with the number of items.

Example

Imagine a row of books on a shelf where each book has a sticky note saying where the next book is. To put a new book in the right alphabetical spot, you have to walk along the row, checking each book until you find the place. The longer the row, the longer the walk, which is the same slowdown that happens when a linked list grows.

3 checked practice questions for this part are in the app.

Part 9 · 1:20:00–1:30:00

This part explains binary search trees, a way to store numbers in a two-branch tree so you can find any number quickly, and why the tree's shape matters.

Key ideas

Words to know

node
A single box in a data structure that holds a value and points to other boxes.
pointer
A stored memory address that tells the program where to find another node.
binary search tree
A tree where each node has at most two children, with smaller values on the left and larger values on the right.
balanced
Evenly shaped, so the tree is not lopsided and each search can skip about half of the remaining nodes.

Example

Think of a guessing game where someone picks a number from 1 to 100 and you guess. If they say higher or lower, each answer lets you throw away half the possible numbers, just like a binary search tree lets you skip half of the tree at each step. If you guessed 1, 2, 3 in order every time, you would get no help from the answers and would have to check each number one by one, which is like a tree that has become a straight line.

3 checked practice questions for this part are in the app.

Part 10 · 1:30:00–1:40:00

This part explains how a dictionary stores pairs of keys and values, and how hashing and hash tables aim to find items quickly.

Key ideas

Words to know

dictionary
A way to store information as pairs, where each key points to one value, like a word and its definition.
hashing
A technique that takes an input, such as a name, and turns it into one of a fixed set of outputs, such as a spot in a list.
hash table
A structure that uses hashing to pick a spot in an array for each key, so the item can be found quickly.
collision
What happens when two different keys are sent to the same spot, such as two friends whose names start with the same letter.

Example

Think of sorting a box of mixed playing cards into four piles by suit. You do not check every card each time you want one. You go straight to the right pile, then look only through that smaller pile. This is the idea behind hashing and hash tables.

3 checked practice questions for this part are in the app.

Part 11 · 1:40:00–1:50:00

This part explains how a hash table stores names in buckets, why crowded buckets can make it slow, and how a bigger table and a hash function help and cost something.

Key ideas

Words to know

hash table
A list of slots where a function decides which slot each item goes into, so the item can be found again quickly.
collision
When two different items are sent to the same slot.
hash function
A rule that takes an input, such as a name, and gives back a slot number.
Big O
A way of describing how the time an algorithm takes grows as the amount of data grows. O(1) means constant time, and O(n) means the time grows with the number of items.

Example

Imagine a coat check where coats are sorted into hooks by the first letter of the owner's last name. If many people have last names starting with the same letter, one hook gets crowded and finding a coat takes longer. Adding more hooks, one for each pair or triple of letters, spreads people out but leaves many hooks empty.

3 checked practice questions for this part are in the app.

Part 12 · 1:50:00–2:02:19

This part explains how hash tables and tries store and find items quickly, and what each one costs.

Key ideas

Words to know

hash function
A rule that turns an input, like a name, into a number that says where to store it.
dictionary
A collection of key and value pairs, like a contact list where each name leads to a phone number and email address.
trie
A tree-shaped way to store words where each letter of a word is a step down the tree.
linked list
A chain of items where each item points to the next one, so you must check them one by one to find something.

Example

Think of a coat check where coats are put on hooks by the first letter of the owner's last name. If coats are spread evenly, you find yours quickly. If one letter gets almost all the coats, you end up searching a long pile, just like a slow hash table.

3 checked practice questions for this part are in the app.

← Week 4: Memory

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. Harvard CS50 · CC BY-NC-SA 4.0 · not affiliated with or endorsed by Harvard. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York