linked lists · 2021. 2. 26. · linked lists at a glance 1 137 3 a linked list is a data structure...
TRANSCRIPT
![Page 1: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/1.jpg)
Linked ListsPart One
![Page 2: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/2.jpg)
Outline for Today
● Linked Lists, Conceptually● A different way to represent a sequence.
● Linked Lists, In Code● Some cool new C++ tricks.
![Page 3: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/3.jpg)
Changing Offices
![Page 4: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/4.jpg)
Dr. Cynthia Lee is no longer in room 100.
She can be found in room 108.
The Sign on Room 100
Room100
Room108
![Page 5: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/5.jpg)
Dr. Cynthia Lee is no longer in room 108.
She can be found in room 190.
The Sign on Room 108
Room100
Room108
Room190
![Page 6: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/6.jpg)
Dr. Cynthia Lee is no longer in room 190.
She can be found in room 192.
The Sign on Room 190
Room100
Room108
Room190
Room192
![Page 7: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/7.jpg)
The Sign on Room 192
Welcome to Cynthia’sOffice!
Room100
Room108
Room190
Room192
![Page 8: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/8.jpg)
Linked Lists at a Glance
1 2 3
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.● The elements are then chained together into a
sequence.● The end of the list is marked with some special
indicator.
![Page 9: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/9.jpg)
Linked Lists at a Glance
1 2 3
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.● The elements are then chained together into a
sequence.● The end of the list is marked with some special
indicator.
![Page 10: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/10.jpg)
Linked Lists at a Glance
1 2 3137
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.● The elements are then chained together into a
sequence.● The end of the list is marked with some special
indicator.
![Page 11: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/11.jpg)
Linked Lists at a Glance
1 3137
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.● The elements are then chained together into a
sequence.● The end of the list is marked with some special
indicator.
![Page 12: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/12.jpg)
...an empty list,or...
a single cell... ... that points at another linked list.
A Linked List is Either...
quokka! pudu! dikdik!kudu!
![Page 13: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/13.jpg)
Representing Linked Lists
![Page 14: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/14.jpg)
...an empty list,or...
a single cell... ... that points at another linked list.
A Linked List is Either...
![Page 15: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/15.jpg)
...an empty list,or...
a single cell... ... that points at another linked list.
A Linked List is Either...
![Page 16: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/16.jpg)
a single cell... ... that points at another linked list.
struct Cell { string value; Cell* next;};
Hi Mom!
![Page 17: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/17.jpg)
struct Cell { string value; Cell* next;};
quokka! pudu! dikdik!kudu!
![Page 18: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/18.jpg)
struct Cell { string value; Cell* next;};
Cell* list = new Cell;
We just want a single cell, not an array of cells. To get the space we need, we’ll just say new Cell.
We just want a single cell, not an array of cells. To get the space we need, we’ll just say new Cell.
Notice that list is still a Cell*, a pointer to a cell.
It still says “look over there for your Cell”
rather than “I’m a Cell!”
Notice that list is still a Cell*, a pointer to a cell.
It still says “look over there for your Cell”
rather than “I’m a Cell!”
Yes, it’s confusing that C++ uses the same types to mean “look over there for an array
of Cells” and “look over there for a single Cell.”
Yes, it’s confusing that C++ uses the same types to mean “look over there for an array
of Cells” and “look over there for a single Cell.”
list
value
next
![Page 19: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/19.jpg)
struct Cell { string value; Cell* next;};
Cell* list = new Cell;list->value = "dikdik!";
Because list is a pointer to a Cell, we use the arrow operator -> instead of the
dot operator.
Think of list->value as saying “start at list,
follow an arrow, then pick the value field.”
Because list is a pointer to a Cell, we use the arrow operator -> instead of the
dot operator.
Think of list->value as saying “start at list,
follow an arrow, then pick the value field.”dikdik!
list
value
next
![Page 20: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/20.jpg)
struct Cell { string value; Cell* next;};
Cell* list = new Cell;list->value = "dikdik!";list->next = new Cell;list->next->value = "quokka!";list->next->next = new Cell;list->next->next->value = "pudu!";list->next->next->next = nullptr;
dikdik!
list
quokka! pudu!
value value value
next next next
![Page 21: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/21.jpg)
struct Cell { string value; Cell* next;}; Cell* list = new Cell;list->value = "dikdik!";list->next = new Cell;list->next->value = "quokka!";list->next->next = new Cell;list->next->next->value = "pudu!";list->next->next->next = nullptr;
dikdik!
list
quokka! pudu!
C++ uses the nullptr keyword to mean “a pointer
that doesn’t point at anything.”
(Older code uses NULL instead of nullptr; that’s also okay,
but we recommend nullptr.)
C++ uses the nullptr keyword to mean “a pointer
that doesn’t point at anything.”
(Older code uses NULL instead of nullptr; that’s also okay,
but we recommend nullptr.)
value value value
next next next
![Page 22: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/22.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 23: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/23.jpg)
Measuring a Linked List
![Page 24: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/24.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 25: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/25.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
dikdik! quokka! pudu!
![Page 26: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/26.jpg)
Printing a Linked List
![Page 27: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/27.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 28: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/28.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
dikdik! quokka! pudu!
![Page 29: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/29.jpg)
What happens if we switchthe order of these two lines?
Formulate a hypothesis,but don’t post anything
in chat just yet. 😃
What happens if we switchthe order of these two lines?
Formulate a hypothesis,but don’t post anything
in chat just yet. 😃
![Page 30: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/30.jpg)
What happens if we switchthe order of these two lines?
Now, post your hypothesisin chat. Not sure? Just
post “??.” 😃
What happens if we switchthe order of these two lines?
Now, post your hypothesisin chat. Not sure? Just
post “??.” 😃
![Page 31: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/31.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
dikdik! quokka! pudu!
![Page 32: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/32.jpg)
Time-Out for Announcements!
![Page 33: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/33.jpg)
Assignment 7
● Assignment 6 was due today at 11:30AM.● Grace period for late submissions ends Sunday at
11:30AM Pacific time.● Assignment 7 (The Great Stanford Hash-Off)
goes out today. It’s due next Friday.● Implement linear probing and Robin Hood hashing!● See how fast these approaches are and how they
compare against chained hashing!● As always, come talk to us if you have any
questions! That’s what we’re here for.
![Page 34: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/34.jpg)
lecture = lecture->next;
![Page 35: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/35.jpg)
Building a Linked List(without hardcoding it)
![Page 36: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/36.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 37: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/37.jpg)
Cleaning Up a Linked List
![Page 38: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/38.jpg)
Endearing C++ Quirks
● If you allocate memory using the new[] operator (e.g. new int[137]), you have to free it using the delete[] operator.
delete[] ptr;
● If you allocate memory using the new operator (e.g. new Cell), you have to free it using the delete operator.
delete ptr;
● Make sure to use the proper deletion operation. Mixing these up is like walking off the end of an array or using an uninitialized pointer; it might work, or it might instantly crash your program, etc.
![Page 39: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/39.jpg)
Cleaning Up Memory
● To free a linked list, we can’t just do this:
delete list;
● Why not?
Formulate a hypothesis,but don’t post anything
in chat just yet. 😃
Formulate a hypothesis,but don’t post anything
in chat just yet. 😃
![Page 40: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/40.jpg)
Cleaning Up Memory
● To free a linked list, we can’t just do this:
delete list;
● Why not?
Now, post yourhypothesis in chat. 😃
Now, post yourhypothesis in chat. 😃
![Page 41: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/41.jpg)
Cleaning Up Memory
● To free a linked list, we can’t just do this:
delete list;
● Why not?
list
Quokka PuduGerenuk
delete
Dynamic
Deallocation!
![Page 42: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/42.jpg)
Cleaning Up Memory
● To free a linked list, we can’t just do this:
delete list;
● Why not?
list
Quokka Pudu
![Page 43: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/43.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 44: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/44.jpg)
What’s wrong with thiscode?
Formulate a hypothesis,but don’t post anything
in chat just yet. 😃
What’s wrong with thiscode?
Formulate a hypothesis,but don’t post anything
in chat just yet. 😃
![Page 45: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/45.jpg)
What’s wrong with thiscode?
Now, post your hypothesisin chat. Not sure? Just
post “??.” 😃
What’s wrong with thiscode?
Now, post your hypothesisin chat. Not sure? Just
post “??.” 😃
![Page 46: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/46.jpg)
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 47: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/47.jpg)
list
Quokka PuduGerenuk
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 48: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/48.jpg)
list
Quokka PuduGerenuk
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 49: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/49.jpg)
list
Quokka PuduGerenuk
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 50: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/50.jpg)
list
Quokka PuduGerenuk
Watch Out!
delete
Dynamic
Deallocation!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 51: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/51.jpg)
list
Quokka Pudu
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 52: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/52.jpg)
list
Quokka Pudu
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 53: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/53.jpg)
list
Quokka Pudu
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);}
![Page 54: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/54.jpg)
list
Quokka Pudu
Watch Out!
void deleteList(Cell* list) { if (list == nullptr) return;
delete list; deleteList(list->next);} Undefined
behavior!
![Page 55: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/55.jpg)
In the Land of C++, wedo not speak to the dead.
What should we do instead?
![Page 56: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/56.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 57: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/57.jpg)
list
Quokka PuduGerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 58: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/58.jpg)
list
Quokka PuduGerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 59: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/59.jpg)
list
Quokka PuduGerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 60: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/60.jpg)
list
Quokka PuduGerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 61: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/61.jpg)
Quokka PuduGerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
list
![Page 62: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/62.jpg)
Quokka Pudu
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
Gerenuk
list
Recursion!
![Page 63: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/63.jpg)
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
Gerenuk
list
![Page 64: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/64.jpg)
list
Gerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 65: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/65.jpg)
list
Gerenuk
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 66: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/66.jpg)
list
Gerenuk
One Option
delete
Dynamic
Deallocation!
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 67: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/67.jpg)
list
One Option
void deleteList(Cell* list) { if (list == nullptr) return;
deleteList(list->next); delete list;}
![Page 68: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/68.jpg)
Your Action Items
● Read Chapter 12.1 – 12.3.● There’s lots of useful information in there
about how to work with linked lists.● Start Assignment 7
● Start working on linear probing. As always, come talk to us if you have any questions!
![Page 69: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the](https://reader035.vdocuments.net/reader035/viewer/2022071410/61041f1c52015b3d977a1f0f/html5/thumbnails/69.jpg)
Next Time
● Linked Lists, Iteratively● How do you manually walk a linked list?
● Pointers Into Lists● Getting a helping hand.