linked lists - web.stanford.edu€¦ · linked lists 1 2 137 3 a linked list is a data structure...
TRANSCRIPT
![Page 1: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/1.jpg)
Linked ListsPart Two
![Page 2: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/2.jpg)
Recap from Last Time
![Page 3: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/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 - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/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...
![Page 5: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/5.jpg)
New Stuff!
![Page 6: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/6.jpg)
A Problem
![Page 7: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/7.jpg)
Stack Overflows
● Recursive code can result in stack overflows in cases where the recursion requires too many stack frames to finish a calculation.
● This means that recursion might not be the best strategy for manipulating linked lists, especially if those lists get really long.
● What should we do instead?
![Page 8: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/8.jpg)
Processing Lists Iteratively
![Page 9: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/9.jpg)
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
![Page 10: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/10.jpg)
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
![Page 11: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/11.jpg)
list result
quokka! dikdik!pudu!
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
list
![Page 12: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/12.jpg)
list result
quokka! dikdik!pudu!
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
list
![Page 13: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/13.jpg)
list result
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
list
quokka! dikdik!pudu!
![Page 14: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/14.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
![Page 15: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/15.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
![Page 16: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/16.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
0
result
![Page 17: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/17.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
0
result
![Page 18: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/18.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
0
result
![Page 19: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/19.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
1
result
![Page 20: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/20.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
1
result
![Page 21: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/21.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
1
result
![Page 22: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/22.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
1
result
![Page 23: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/23.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
1
result
![Page 24: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/24.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
1
result
![Page 25: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/25.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
2
result
![Page 26: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/26.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
2
result
![Page 27: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/27.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
2
result
![Page 28: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/28.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
2
result
![Page 29: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/29.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
2
result
![Page 30: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/30.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
2
result
![Page 31: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/31.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
3
result
![Page 32: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/32.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
3
result
![Page 33: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/33.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
3
result
![Page 34: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/34.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
3
result
![Page 35: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/35.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
3
result
![Page 36: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/36.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}
list
3
result
![Page 37: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/37.jpg)
list result
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
list
quokka! dikdik!pudu!
![Page 38: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/38.jpg)
list result
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
int main() { Cell* list = readList(); cout << lengthOf(list) << endl;
/* … other listy things. … */}
list
quokka! dikdik!pudu!
![Page 39: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/39.jpg)
Printing a List
![Page 40: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/40.jpg)
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
![Page 41: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/41.jpg)
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
![Page 42: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/42.jpg)
list result
quokka! dikdik!pudu!
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
![Page 43: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/43.jpg)
list result
quokka! dikdik!pudu!
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
![Page 44: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/44.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
![Page 45: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/45.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 46: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/46.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 47: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/47.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 48: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/48.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 49: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/49.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 50: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/50.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 51: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/51.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 52: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/52.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 53: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/53.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 54: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/54.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 55: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/55.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 56: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/56.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 57: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/57.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 58: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/58.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}
list
![Page 59: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/59.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
![Page 60: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/60.jpg)
list result
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
int main() { Cell* list = readList(); printList(list);
/* … other listy things. … */}
list
quokka! dikdik!pudu!
![Page 61: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/61.jpg)
Freeing a Linked List, Iteratively
![Page 62: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/62.jpg)
First, the Wrong Way
![Page 63: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/63.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG while (list != nullptr) { delete list; list = list->next; }} list
![Page 64: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/64.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }} list
![Page 65: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/65.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }} list
![Page 66: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/66.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }} list
delete
Dynamic
Deallocation!
![Page 67: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/67.jpg)
quokka! dikdik!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }} list
![Page 68: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/68.jpg)
quokka! dikdik!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }} list
![Page 69: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/69.jpg)
quokka! dikdik!
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }}
void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG
while (list != nullptr) { delete list; list = list->next; }} list
![Page 70: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/70.jpg)
void deleteList(Cell* list) { while (list != nullptr) {
delete list; list = list->next; }}
void deleteList(Cell* list) { while (list != nullptr) {
delete list; list = list->next; }}
quokka! dikdik!pudu!
list
![Page 71: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/71.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = list->next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = list->next; }}
list
![Page 72: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/72.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 73: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/73.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 74: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/74.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 75: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/75.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 76: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/76.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 77: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/77.jpg)
quokka! dikdik!pudu!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 78: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/78.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 79: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/79.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 80: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/80.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 81: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/81.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 82: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/82.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 83: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/83.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 84: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/84.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 85: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/85.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 86: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/86.jpg)
quokka! dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 87: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/87.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 88: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/88.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 89: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/89.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 90: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/90.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 91: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/91.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 92: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/92.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 93: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/93.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 94: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/94.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 95: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/95.jpg)
dikdik!
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 96: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/96.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 97: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/97.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 98: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/98.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 99: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/99.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list next
![Page 100: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/100.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 101: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/101.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 102: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/102.jpg)
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}
list
![Page 103: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/103.jpg)
Pointers Into Lists
● When processing linked lists iteratively, it’s common to introduce pointers that point to cells in multiple spots in the list.
● This is particularly useful if we’re destroying or rewiring existing lists.
quokka! dikdik!pudu!
list next
![Page 104: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/104.jpg)
Building a Linked List
![Page 105: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/105.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
![Page 106: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/106.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
![Page 107: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/107.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
result
![Page 108: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/108.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
result
![Page 109: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/109.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
result
![Page 110: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/110.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
result
![Page 111: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/111.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
result
![Page 112: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/112.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
result
![Page 113: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/113.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
result
![Page 114: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/114.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
result
![Page 115: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/115.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell result
![Page 116: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/116.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
???
result
![Page 117: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/117.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
???
result
![Page 118: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/118.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
???
result
![Page 119: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/119.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
???
result
![Page 120: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/120.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
???
result
![Page 121: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/121.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
???
result
![Page 122: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/122.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
???
result
![Page 123: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/123.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
result
![Page 124: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/124.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
result
![Page 125: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/125.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
result
![Page 126: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/126.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
result
![Page 127: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/127.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!line
cell
dikdik!
result
![Page 128: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/128.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!
result
![Page 129: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/129.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!
result
![Page 130: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/130.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
dikdik!
result
![Page 131: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/131.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
dikdik!
result
![Page 132: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/132.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
dikdik!
result
![Page 133: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/133.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
dikdik!
result
![Page 134: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/134.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
???
dikdik!
result
![Page 135: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/135.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
???
dikdik!
result
![Page 136: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/136.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
quokka!
???
dikdik!
result
![Page 137: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/137.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
quokka!
???
dikdik!
result
![Page 138: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/138.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
quokka! dikdik!
result
![Page 139: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/139.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
quokka! dikdik!
result
![Page 140: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/140.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
quokka! dikdik!
result
![Page 141: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/141.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka!line
cell
quokka! dikdik!
result
![Page 142: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/142.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka! dikdik!
result
![Page 143: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/143.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka! dikdik!
result
![Page 144: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/144.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka! dikdik!
result
![Page 145: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/145.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
result
![Page 146: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/146.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
result
![Page 147: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/147.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
result
![Page 148: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/148.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
???
![Page 149: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/149.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
???
![Page 150: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/150.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
pudu!
???
![Page 151: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/151.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
pudu!
???
![Page 152: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/152.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
pudu!
![Page 153: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/153.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
pudu!
![Page 154: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/154.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
pudu!
![Page 155: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/155.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
resultcell
pudu!
![Page 156: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/156.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
pudu!
line
quokka! dikdik!
result
pudu!
![Page 157: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/157.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka! dikdik!
result
pudu!
It’s a bug: these elements are in the wrong order!
It’s a feature: we just implemented a stack using
linked lists!
It’s a bug: these elements are in the wrong order!
It’s a feature: we just implemented a stack using
linked lists!
![Page 158: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/158.jpg)
Cell* result = nullptr;while (true) {
string line = getLine("Next entry? ");if (line == "") break;
Cell* cell = new Cell;cell->value = line;
cell->next = result;result = cell;
}return result;
quokka! dikdik!
result
pudu!
It’s a bug: these elements are in the wrong order!
It’s a feature: we just implemented a stack using
linked lists!
It’s a bug: these elements are in the wrong order!
It’s a feature: we just implemented a stack using
linked lists!
![Page 159: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/159.jpg)
Your Action Items
● Read Chapter 12.1 – 12.3.● It’s a good overview of linked lists.
● Finish Assignment 6.● Need help? Come talk to us! That’s what
we’re here for.
![Page 160: Linked Lists - web.stanford.edu€¦ · 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 rest](https://reader034.vdocuments.us/reader034/viewer/2022050208/5f5ae25a4ce60631f63d9f29/html5/thumbnails/160.jpg)
Next Time
● Tail Pointers● Tracking the start and end of a list.
● Implementing the Queue● A Tale of Two Implementations.
● Variations on Linked Lists● What linked lists look like “in the wild.”