Section 5 - Data Structure
Section 5 - Data Structure
Think.
Pair.
Share.
● What are the key trade-offs between data structures
we should consider in decisions about which to use?
● What are some of the primary operations we should
know how to do on a linked list?
● How can we build our very own hash table?
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?
Deletion
Insertion
Search
1. Search
2. Insertion
3. Deletion
1. Insertion
2. Search
3. Deletion
Linked List
list
"Hey!" "Hi!"
NULL
…
H "Hey!" "Hello!"
L "Lo there!"
…
Hash Table
Trie L L O
H E P
Y
Trade-offs
Linked List
list
"Hey!" "Hi!"
NULL
Nodes
typedef struct node
{
string phrase;
struct node *next;
}
node;
node
typedef struct node
{
string phrase;
struct node *next;
}
node;
node
typedef struct node
{
string phrase; phrase
struct node *next;
}
node;
node
typedef struct node
{
string phrase; "Hi!" phrase
struct node *next;
}
node;
node
typedef struct node
{
string phrase; "Bye!" phrase
struct node *next;
}
node;
node
typedef struct node
{
string phrase; phrase
struct node *next;
}
node; next
node
typedef struct node
{
string phrase; phrase
struct node *next;
}
node; 0x123 next
node
typedef struct node
{
string phrase; phrase
struct node *next;
}
node; 0x456 next
node
typedef struct node
{
string phrase; phrase
struct node *next;
}
node; next
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));
list n
node *n = malloc(sizeof(node));
n->phrase = "Hi!";
list n
"Hi!"
node *n = malloc(sizeof(node));
n->phrase = "Hi!";
n->next = NULL;
list n
"Hi!"
NULL
list = n;
list n
"Hi!"
NULL
list = n;
list
n
"Hi!"
NULL
Inserting Nodes
n = malloc(sizeof(node));
list
"Hi!"
NULL
n = malloc(sizeof(node));
n list
"Hi!"
NULL
n = malloc(sizeof(node));
n->phrase = "Hey!";
n list
"Hey!" "Hi!"
NULL
n = malloc(sizeof(node));
n->phrase = "Hey!";
n->next = list;
n list
"Hey!" "Hi!"
NULL
list = n;
n list
"Hey!" "Hi!"
NULL
list = n;
list
"Hey!" "Hi!"
NULL
Inserting into a Linked List
"Hey!" "Hi!"
NULL
free(list);
list
"Hey!" "Hi!"
NULL
free(list);
list
"Hey!" "Hi!"
NULL
list
"Hey!" "Hi!"
NULL
node *ptr = list->next;
list ptr
"Hey!" "Hi!"
NULL
free(list);
list ptr
"Hey!" "Hi!"
NULL
list = ptr;
list ptr
"Hey!" "Hi!"
NULL
list = ptr;
list ptr
"Hi!"
NULL
ptr = list->next;
list ptr
"Hi!"
NULL
ptr = list->next;
list
"Hi!"
NULL
free(list);
list
"Hi!"
NULL
list = ptr;
list
Unloading a Linked List
H "Hey!" "Hello!"
L "Lo there!"
…
… …
7 H "Hey!" "Hello!"
8 I
9 J
10 K
11 L "Lo there!"
… …
"Hey!" Hash Function 7
Hashing
Always gives you the same value for the same input
Produces an even distribution across buckets
Uses all buckets
This was CS50