This is CS50
Agenda
Deletion
Insertion
Search
"Hi!"
NULL
"Hey!"
list
Linked List
… |
H |
I |
J |
K |
L |
… |
"Hey!"
"Hello!"
"Lo there!"
Hash Table
H
E
L
L
O
Y
Trie
P
Trade-offs
Nodes
typedef struct node
{
string phrase;
struct node *next;
}
node;
typedef struct node
{
string phrase;
struct node *next;
}
node;
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
"Hi!"
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
"Bye!"
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
next
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
0x123
next
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
0x456
next
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
phrase
next
node
Creating a Linked List
node *list = NULL;
list
node *n = malloc(sizeof(node));
list
node *n = malloc(sizeof(node));
list
node *n = malloc(sizeof(node));
n
list
node *n = malloc(sizeof(node));
n->phrase = "Hi!";
"Hi!"
n
list
node *n = malloc(sizeof(node));
n->phrase = "Hi!";
n->next = NULL;
"Hi!"
NULL
n
list
list = n;
"Hi!"
NULL
n
list
list = n;
"Hi!"
NULL
n
list
Inserting Nodes
n = malloc(sizeof(node));
"Hi!"
NULL
list
n = malloc(sizeof(node));
"Hi!"
NULL
list
n
n = malloc(sizeof(node));
n->phrase = "Hey!";
"Hi!"
NULL
list
"Hey!"
n
n = malloc(sizeof(node));
n->phrase = "Hey!";
n->next = list;
"Hi!"
NULL
list
"Hey!"
n
list = n;
"Hi!"
NULL
"Hey!"
list
n
list = n;
"Hi!"
NULL
"Hey!"
list
"Hi!"
NULL
"Hey!"
list
free(list);
"Hi!"
NULL
"Hey!"
list
free(list);
"Hi!"
NULL
"Hey!"
list
"Hi!"
NULL
"Hey!"
list
node *ptr = list->next;
"Hi!"
NULL
"Hey!"
list
ptr
free(list);
"Hi!"
NULL
"Hey!"
ptr
list
list = ptr;
"Hi!"
NULL
"Hey!"
ptr
list
list = ptr;
"Hi!"
NULL
ptr
list
ptr = list->next;
"Hi!"
NULL
ptr
list
ptr = list->next;
"Hi!"
NULL
list
free(list);
"Hi!"
NULL
list
list = ptr;
list
Inserting and Unloading a Linked List
Download and open list.c at cs50.ly/supersection1.
"Hey!"
"Hello!"
"Lo there!"
… |
H |
I |
J |
K |
L |
… |
"Hey!"
"Hello!"
"Lo there!"
… | … |
7 | H |
8 | I |
9 | J |
10 | K |
11 | L |
… | … |
"Hey!"
"Hello!"
"Lo there!"
Hash Function
"Hey!"
7
Hashing
Download and open table.c at cs50.ly/supersection2.
TODO: complete hash to return a number, 0–25, depending on the first character in the word.
We will walk through it together!
A good hash function…
Always gives you the same value for the same input
Produces an even distribution across buckets
Uses all buckets
Inheritance
This was CS50