linked lists - stanford university€¦ · linked lists 1 2 137 3 a linked list is a data structure...
TRANSCRIPT
![Page 1: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/1.jpg)
Linked ListsPart Two
![Page 2: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/2.jpg)
Recap from Last Time
![Page 3: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/3.jpg)
Linked Lists
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 4: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/4.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
struct Cell { Type data; Cell* next;};
struct Cell { Type data; Cell* next;};
![Page 5: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/5.jpg)
Processing Lists Recursively
![Page 6: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/6.jpg)
Processing Lists Iteratively
![Page 7: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/7.jpg)
Linked Lists, Iteratively
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
1 2 43list
![Page 8: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/8.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 9: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/9.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 10: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/10.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 11: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/11.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 12: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/12.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 13: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/13.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 14: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/14.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 15: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/15.jpg)
Linked Lists, Iteratively
1 2 43
curr
● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {
/* … do something with curr->value … */
}
list
![Page 16: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/16.jpg)
New Stuff!
![Page 17: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/17.jpg)
ap·op·to·sis
the death of cells whichoccurs as a normaland controlled part
of an organism's growthor development.
Source: Google
![Page 18: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/18.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 leads to Undefined Behavior.
![Page 19: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/19.jpg)
Freeing a Linked List
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {
delete ptr;
}
![Page 20: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/20.jpg)
Freeing a Linked List
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {
delete ptr;
}
![Page 21: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/21.jpg)
Freeing a Linked List
2 ...ptr
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {
delete ptr;
}
![Page 22: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/22.jpg)
Freeing a Linked List
2 ...ptr
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {
delete ptr;
}
![Page 23: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/23.jpg)
Freeing a Linked List
???ptr
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {
delete ptr;
}
![Page 24: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/24.jpg)
Freeing a Linked List Properly
● To properly free a linked list, we have to be able to● destroy a cell, and● advance to the cell after it.
● How might we accomplish this?
![Page 25: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/25.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
![Page 26: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/26.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
pudu!
![Page 27: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/27.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
pudu!
![Page 28: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/28.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
pudu!
![Page 29: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/29.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
pudu!
next
![Page 30: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/30.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
pudu!
next
![Page 31: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/31.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
pudu!
next
poof!
![Page 32: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/32.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
???
next
![Page 33: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/33.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
???
next
![Page 34: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/34.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
next
![Page 35: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/35.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
next
![Page 36: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/36.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
![Page 37: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/37.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
![Page 38: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/38.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
![Page 39: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/39.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
next
![Page 40: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/40.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
quokka! dikdik!
list
next
![Page 41: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/41.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
???dikdik!
list
next
![Page 42: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/42.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
???dikdik!
list
next
![Page 43: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/43.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
next
![Page 44: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/44.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
next
![Page 45: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/45.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
![Page 46: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/46.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
![Page 47: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/47.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
![Page 48: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/48.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
next
![Page 49: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/49.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
dikdik!
list
next
![Page 50: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/50.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
???
list
next
![Page 51: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/51.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
???
list
next
![Page 52: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/52.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
list
next
![Page 53: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/53.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
list
next
![Page 54: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/54.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
list
![Page 55: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/55.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
list
![Page 56: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/56.jpg)
while (list != nullptr) {Cell* next = list->next;
delete list; list = next;}
list
![Page 57: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/57.jpg)
...an empty list,represented bynullptr, or...
a single linked listcell that points...
... at another linked list.
A Linked List is Either...
![Page 58: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/58.jpg)
Pointers and References
![Page 59: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/59.jpg)
Prepending an Element
● Suppose that we want to write a function that will add an element to the front of a linked list.
● What might this function look like?
sunfish elephantwhale
list
![Page 60: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/60.jpg)
Prepending an Element
● Suppose that we want to write a function that will add an element to the front of a linked list.
● What might this function look like?
arapaima sunfish elephantwhale
list
![Page 61: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/61.jpg)
Prepending an Element
● Suppose that we want to write a function that will add an element to the front of a linked list.
● What might this function look like?
arapaima sunfish elephantwhale
list
![Page 62: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/62.jpg)
What went wrong?
![Page 63: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/63.jpg)
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
![Page 64: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/64.jpg)
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
![Page 65: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/65.jpg)
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
list
![Page 66: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/66.jpg)
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
list
![Page 67: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/67.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list Sartrevalue
![Page 68: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/68.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list Sartrevalue
![Page 69: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/69.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list SartrevaluenewCell
![Page 70: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/70.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
list SartrevaluenewCell
![Page 71: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/71.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
list SartrevaluenewCell
![Page 72: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/72.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
Sartre
???
list SartrevaluenewCell
![Page 73: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/73.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
Sartre
???
SartrevaluenewCell
![Page 74: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/74.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
Sartre
SartrevaluenewCell
![Page 75: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/75.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
Sartre
SartrevaluenewCell
![Page 76: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/76.jpg)
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
Sartre
SartrevaluenewCell
![Page 77: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/77.jpg)
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
list
Sartre
![Page 78: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/78.jpg)
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");
return 0;}
list
Sartre
Hell is other pointers
![Page 79: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/79.jpg)
Pointers by Reference
● To resolve this problem, we can pass the linked list pointer by reference.
● Our new function:
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
![Page 80: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/80.jpg)
Pointers by Reference
● To resolve this problem, we can pass the linked list pointer by reference.
● Our new function:
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
![Page 81: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/81.jpg)
Pointers by Reference
● To resolve this problem, we can pass the linked list pointer by reference.
● Our new function:
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;} This is a reference to a
pointer to a Cell. If we change where list points in this function,
the changes will stick!
This is a reference to a pointer to a Cell. If we change where list points in this function,
the changes will stick!
![Page 82: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/82.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
![Page 83: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/83.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
![Page 84: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/84.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
![Page 85: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/85.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
![Page 86: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/86.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
listThe
Turtlesvalue
![Page 87: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/87.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
listThe
Turtlesvalue
![Page 88: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/88.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
listThe
Turtlesvalue newCell
![Page 89: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/89.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
listThe
Turtlesvalue newCell
![Page 90: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/90.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
listThe
Turtlesvalue newCell
![Page 91: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/91.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
TheTurtles
???
listThe
Turtlesvalue newCell
![Page 92: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/92.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
TheTurtles
???
listThe
Turtlesvalue newCell
![Page 93: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/93.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
TheTurtles
listThe
Turtlesvalue newCell
![Page 94: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/94.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
TheTurtles
listThe
Turtlesvalue newCell
![Page 95: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/95.jpg)
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");
return 0;}
list
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
TheTurtles
listThe
Turtlesvalue newCell
![Page 96: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/96.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
TheTurtles
![Page 97: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/97.jpg)
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");
return 0;}
list
TheTurtles
🎶 So happy 🎶 🎶 together 🎶
![Page 98: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/98.jpg)
Pointers by Reference
● If you pass a pointer into a function by value, you can change the contents at the object you point at, but not which object you point at.
● If you pass a pointer into a function by reference, you can also change which object is pointed at.
![Page 99: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/99.jpg)
Time-Out for Announcements!
![Page 100: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/100.jpg)
Midterm Timetable
● You’re done with the midterm exam! Woohoo!
● We’ll be grading it over the weekend and returning graded exams on Monday along with stats and solutions.
● Have any questions in the meantime? Just ask!
![Page 101: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/101.jpg)
Assignment 5
● Assignment 5 (Data Sagas) is due one week from today.
● We assume most of you have not yet started, and that’s fine. Start working through that assignment this evening and make slow and steady progress.
● Have questions? Stop by the LaIR or CLaIR!
![Page 102: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/102.jpg)
lecture = announcements->next;
![Page 103: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/103.jpg)
Implementing the Queue
![Page 104: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/104.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
![Page 105: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/105.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
gerenuk
![Page 106: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/106.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
gerenuk impala
![Page 107: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/107.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
gerenuk impala greaterkudu
![Page 108: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/108.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
gerenuk impala greaterkudu
alpaca
![Page 109: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/109.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
gerenuk impala greaterkudu
alpaca slowloris
![Page 110: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/110.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
impala greaterkudu
alpaca slowloris
![Page 111: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/111.jpg)
Implementing the Queue
● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of
the list.● Question: How efficient is this?
greaterkudu
alpaca slowloris
![Page 112: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/112.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
1 2 3
head
![Page 113: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/113.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
1 2 3
head tail
![Page 114: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/114.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
1 2 3
head tail
4
![Page 115: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/115.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
1 2 3
head tail
4
![Page 116: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/116.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
2 3
head tail
4
![Page 117: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/117.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
3
head tail
4
![Page 118: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/118.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
head tail
4
![Page 119: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/119.jpg)
Tail Pointers
● A tail pointer is a pointer to the last element of a linked list.
● Tail pointers make it easy and efficient to add new elements to the back of a linked list.
● We can use tail pointers to implement an efficient Queue using a linked list.
head tail
![Page 120: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/120.jpg)
class OurQueue {public: OurQueue(); ~OurQueue();
int peek() const; void enqueue(int value); int dequeue();
int size() const; bool isEmpty() const;
private: struct Cell { int value; Cell* next; };
Cell* head; Cell* tail;};
![Page 121: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/121.jpg)
Enqueuing Things
● Case 1: The queue is empty.
![Page 122: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/122.jpg)
Enqueuing Things
● Case 1: The queue is empty.
head tail
![Page 123: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/123.jpg)
Enqueuing Things
● Case 1: The queue is empty.
1
head tail
![Page 124: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/124.jpg)
Enqueuing Things
● Case 1: The queue is empty.
head tail
1
![Page 125: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/125.jpg)
Enqueuing Things
● Case 1: The queue is empty.
● Case 2: The queue is not empty.
head tail
1 3 7
head tail
1
![Page 126: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/126.jpg)
Enqueuing Things
● Case 1: The queue is empty.
● Case 2: The queue is not empty.
head tail
1 3 7
head tail
1
137
![Page 127: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/127.jpg)
Enqueuing Things
● Case 1: The queue is empty.
● Case 2: The queue is not empty.
head tail
1 3 7
head tail
1
137
![Page 128: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/128.jpg)
Dequeuing Things
● Case 1: Dequeuing when there are 2+ elements.
![Page 129: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/129.jpg)
Dequeuing Things
● Case 1: Dequeuing when there are 2+ elements.
head tail
1 3 7 137
![Page 130: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/130.jpg)
Dequeuing Things
● Case 1: Dequeuing when there are 2+ elements.
head tail
1 3 7 137
![Page 131: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/131.jpg)
Dequeuing Things
● Case 1: Dequeuing when there are 2+ elements.
head tail
3 7 137
![Page 132: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/132.jpg)
Dequeuing Things
● Case 1: Dequeuing when there are 2+ elements.
Case 2: Dequeuing the last element.
head tail
3 7 137
head tail
137
![Page 133: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/133.jpg)
Dequeuing Things
● Case 1: Dequeuing when there are 2+ elements.
Case 2: Dequeuing the last element.
head tail
3 7 137
head tail
![Page 134: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/134.jpg)
The Overall Analysis
● Implementing a queue using a linked list without a tail pointer:● Cost of an enqueue: O(n)● Cost of a dequeue: O(1)
● Implementing a queue using a linked list with a tail pointer:● Cost of an enqueue: O(1)● Cost of a dequeue: O(1)
● This is really, really fast!
![Page 135: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/135.jpg)
Your Action Items
● Read Chapter 12 of the textbook (and, optionally, Chapter 13).● It’ll provide more information about linked
lists, data structure implementation, and runtime efficiency.
● Work on Assignment 5.● At a bare minimum, read through the handout
and make sure you know what’s asked of you.● Recommendation: Complete multiway merge
and start lower bound searching by next time.
![Page 136: Linked Lists - Stanford University€¦ · Linked Lists 1 2 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.us/reader035/viewer/2022062917/5ed6e3fedf0eda5e752ae8c7/html5/thumbnails/136.jpg)
Next Time
● Tree Structures● Encoding trees directly in software!
● Binary Search Trees● A fast, flexible, powerful data structure.