This is CS50
Think.
Pair.
Share.
Scenario
Imagine you work for a company that has created a digital assistant running on a mobile device.
Customer reports indicate people have trouble activating the assistant with its "wake word".
Your team has been asked to ensure the voice assistant can be awoken with a greater variety of words.
What data structure would you propose the team build to store these words?
"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
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
Inserting into a Linked List
Download and open list.c.
Find the first TODO.
Starting below that TODO, implement code to add a node to the linked list. Ensure that list always points to the head of the linked list. Also ensure your new node contains a phrase.
Unloading Nodes
"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
Unloading a Linked List
Open the same list.c file.
Find the unload function below main.
Implement unload such that all nodes in the linked list are free'd when the function is called. Return true when successful.
"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.
Complete hash to return a number, 0–25, depending on the first character in the word.
A good hash function…
Always gives you the same value for the same input
Produces an even distribution across buckets
Uses all buckets
This was CS50