0% found this document useful (0 votes)
7 views

Section 5 - Data Structure

The document discusses different data structures that could be used to store wake words for a voice assistant, including linked lists and hash tables. It provides examples of how to implement basic operations like insertion, deletion and searching on a linked list, and discusses the tradeoffs between different data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Section 5 - Data Structure

The document discusses different data structures that could be used to store wake words for a voice assistant, including linked lists and hash tables. It provides examples of how to implement basic operations like insertion, deletion and searching on a linked list, and discusses the tradeoffs between different data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

This is CS50

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

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.
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

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 "Hey!" "Hello!"

L "Lo there!"


… …

7 H "Hey!" "Hello!"

8 I

9 J

10 K

11 L "Lo there!"

… …
"Hey!" Hash Function 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

You might also like