linked lists - stanford universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...outline for...
TRANSCRIPT
![Page 1: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/1.jpg)
Linked ListsPart Three
![Page 2: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/2.jpg)
Outline for Today
● Pointers by Reference● Changing where you’re looking.
● Tail Pointers● Speeding up list operations.
● Doubly-Linked Lists● A preview of things to come.
![Page 3: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/3.jpg)
Pointers and References
![Page 4: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/4.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 5: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/5.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 6: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/6.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 7: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/7.jpg)
What went wrong?
![Page 8: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/8.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
![Page 9: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/9.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
![Page 10: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/10.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
![Page 11: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/11.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
![Page 12: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/12.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
![Page 13: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/13.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
value
![Page 14: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/14.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
value
![Page 15: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/15.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
?
![Page 16: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/16.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
?
![Page 17: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/17.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
Sartre
?
![Page 18: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/18.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
Sartre
?
![Page 19: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/19.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
Sartre
![Page 20: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/20.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
Sartre
![Page 21: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/21.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
list
Sartre
valuecell
Sartre
![Page 22: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/22.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
Sartre
![Page 23: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/23.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
Sartre
![Page 24: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/24.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
list
Sartre
Hell is other pointers
![Page 25: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/25.jpg)
Pointers By Value
● Unless specified otherwise, function arguments in C++ are passed by value.
● This includes pointers!● A function that takes a
pointer as an argument gets a copy of the pointer.
● We can change where the copy points, but not where the original pointer points.
pointer inmain
pointer infunction
![Page 26: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/26.jpg)
Pointers by Reference
● To resolve this problem, we can pass the linked list pointer by reference.
● Our new function:
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = list; list = cell;}
![Page 27: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/27.jpg)
Pointers by Reference
● To resolve this problem, we can pass the linked list pointer by reference.
● Our new function:
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = list; list = cell;}
![Page 28: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/28.jpg)
Pointers by Reference
● To resolve this problem, we can pass the linked list pointer by reference.
● Our new function:
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = list; list = cell;} 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 29: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/29.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
![Page 30: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/30.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
![Page 31: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/31.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
list
![Page 32: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/32.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
list
![Page 33: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/33.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
Descartes
value
list
![Page 34: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/34.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
Descartes
value
![Page 35: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/35.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
?
Descartes
value
![Page 36: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/36.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
?
Descartes
value
![Page 37: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/37.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
Descartes
?
Descartes
value
![Page 38: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/38.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
Descartes
?
Descartes
value
![Page 39: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/39.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
Descartes
Descartes
value
![Page 40: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/40.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
Descartes
Descartes
value
![Page 41: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/41.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Sartre"); prependTo(list, "Camus"); prependTo(list, "Nietzsche");
return 0;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}
void prependTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value;
cell->next = list; list = cell;}list
cell
Descartes
Descartes
value
![Page 42: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/42.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
list
Descartes
![Page 43: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/43.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
list
Descartes
![Page 44: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/44.jpg)
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
int main() { Cell* list = nullptr; prependTo(list, "Descartes"); prependTo(list, "Kant"); prependTo(list, "Bentham");
return 0;}
list
Descartes
I link,therefore I am.
![Page 45: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/45.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 46: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/46.jpg)
Appending to a List
![Page 47: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/47.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
alpacaalpaca
![Page 48: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/48.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
alpacaalpaca
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
![Page 49: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/49.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
alpacaalpaca
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
alpacaslowloris
![Page 50: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/50.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
alpacaalpaca
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
alpacaslowloris
![Page 51: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/51.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
alpacaslowloris
alpacaalpaca
![Page 52: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/52.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
alpacaslowloris
alpacaalpaca
![Page 53: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/53.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
alpacaslowloris
alpacaalpaca
![Page 54: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/54.jpg)
Appending to a List
● Think about which link needs to get changed to append something to this list:
gerenuk impala greaterkudu
alpacaalpaca
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
1. Create a cell whose next field is null.2. Find the last cell in the list.3. Change where the last cell points.
alpacaslowloris
![Page 55: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/55.jpg)
What Went Wrong?
![Page 56: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/56.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
![Page 57: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/57.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
![Page 58: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/58.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
![Page 59: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/59.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
![Page 60: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/60.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
![Page 61: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/61.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
list
Last
value
![Page 62: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/62.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
list
Last
value
![Page 63: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/63.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
list
Last
valuecell
Last
![Page 64: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/64.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { list = list->next; }
list->next = cell;}
list
Last
valuecell
Last
![Page 65: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/65.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { // Uh oh! list = list->next; }
list->next = cell;}
void appendTo(Cell* list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list->next != nullptr) { // Uh oh! list = list->next; }
list->next = cell;}
list
Last
valuecell
Last
Null PointerDereference!
![Page 66: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/66.jpg)
Appending to a List
● There’s an edge case we missed! We need to account for the list being empty.
● If the list is empty, we should change the list pointer to point to our new cell.
● Let’s change things up and see if we can fix this problem.
![Page 67: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/67.jpg)
What Went Wrong (This Time)?
![Page 68: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/68.jpg)
What Went Wrong (This Other Time)?
![Page 69: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/69.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
![Page 70: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/70.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
![Page 71: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/71.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
![Page 72: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/72.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
![Page 73: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/73.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
value
![Page 74: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/74.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
value
![Page 75: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/75.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
valuecell
Last
list
![Page 76: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/76.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
valuecell
Last
list
![Page 77: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/77.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
valuecell
Last
list
![Page 78: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/78.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
valuecell
Last
list
![Page 79: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/79.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
valuecell
Last
list
![Page 80: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/80.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Last
valuecell
Last
list
![Page 81: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/81.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
![Page 82: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/82.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
![Page 83: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/83.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
value
![Page 84: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/84.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
value
![Page 85: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/85.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 86: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/86.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 87: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/87.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 88: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/88.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 89: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/89.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 90: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/90.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 91: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/91.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Final
valuecell
Final
![Page 92: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/92.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
Final
![Page 93: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/93.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
Final
![Page 94: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/94.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
![Page 95: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/95.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
![Page 96: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/96.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 97: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/97.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 98: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/98.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 99: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/99.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 100: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/100.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 101: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/101.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 102: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/102.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 103: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/103.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 104: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/104.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 105: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/105.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
while (list != nullptr && list->next != nullptr) { list = list->next; // Uh oh! }
if (list == nullptr) { list = cell; } else { list->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 106: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/106.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
Final Ultimate
![Page 107: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/107.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
list
Final UltimateLast
![Page 108: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/108.jpg)
When passing in pointers by reference,be careful not to change the pointer
unless you really want to change where it’s pointing!
![Page 109: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/109.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
Final
![Page 110: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/110.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
![Page 111: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/111.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
![Page 112: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/112.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 113: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/113.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
![Page 114: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/114.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 115: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/115.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 116: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/116.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 117: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/117.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 118: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/118.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 119: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/119.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 120: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/120.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 121: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/121.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 122: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/122.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 123: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/123.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
void appendTo(Cell*& list, const string& value) { Cell* cell = new Cell; cell->value = value; cell->next = nullptr;
Cell* end = list; while (end != nullptr && end->next != nullptr) { end = end->next; }
if (list == nullptr) { list = cell; } else { end->next = cell; }}
Ultimate
value
Last
list
Final
cell
Ultimate
end
![Page 124: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/124.jpg)
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
int main() { Cell* list = nullptr; appendTo(list, "Last"); appendTo(list, "Final"); appendTo(list, "Ultimate"); appendTo(list, "Terminal");
/* … other listy things. … */}
Last
list
Final Ultimate
![Page 125: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/125.jpg)
What Went Wrong (Yet Again)?
![Page 126: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/126.jpg)
Appending to a List
● What is the big-O complexity of appending to the back of a linked list using our algorithm?
● Answer: O(n), where n is the number of elements in the list, since we have to find the last position each time.
![Page 127: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/127.jpg)
Appending to a List
● What is the big-O complexity of appending to the back of a linked list using our algorithm?
● Answer: O(n), where n is the number of elements in the list, since we have to find the last position each time.
gerenuk
![Page 128: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/128.jpg)
Appending to a List
● What is the big-O complexity of appending to the back of a linked list using our algorithm?
● Answer: O(n), where n is the number of elements in the list, since we have to find the last position each time.
gerenuk impala
![Page 129: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/129.jpg)
Appending to a List
● What is the big-O complexity of appending to the back of a linked list using our algorithm?
● Answer: O(n), where n is the number of elements in the list, since we have to find the last position each time.
gerenuk impala greaterkudu
![Page 130: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/130.jpg)
Appending to a List
● What is the big-O complexity of appending to the back of a linked list using our algorithm?
● Answer: O(n), where n is the number of elements in the list, since we have to find the last position each time.
gerenuk impala greaterkudu
alpaca
![Page 131: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/131.jpg)
Appending to a List
● What is the big-O complexity of appending to the back of a linked list using our algorithm?
● Answer: O(n), where n is the number of elements in the list, since we have to find the last position each time.
gerenuk impala greaterkudu
alpaca slowloris
![Page 132: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/132.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.
1 2 3
head
![Page 133: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/133.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.
1 2 3
head tail
![Page 134: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/134.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.
1 2 3
head tail
4
![Page 135: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/135.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.
1 2 3
head tail
4
![Page 136: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/136.jpg)
Appending Things Quickly
● Case 1: The list is empty.
head tail
![Page 137: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/137.jpg)
Appending Things Quickly
● Case 1: The list is empty.
1
head tail
![Page 138: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/138.jpg)
Appending Things Quickly
● Case 1: The list is empty.
head tail
1
![Page 139: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/139.jpg)
Appending Things Quickly
● Case 1: The list is empty.
● Case 2: The list is not empty.
head tail
1 3 7
head tail
1
![Page 140: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/140.jpg)
Appending Things Quickly
● Case 1: The list is empty.
● Case 2: The list is not empty.
head tail
1 3 7
head tail
1
137
![Page 141: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/141.jpg)
Appending Things Quickly
● Case 1: The list is empty.
● Case 2: The list is not empty.
head tail
1 3 7
head tail
1
137
![Page 142: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/142.jpg)
Coda: Doubly-Linked Lists
![Page 143: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/143.jpg)
Doubly-Linked Lists
● There’s a strange asymmetry in a linked list: you can easily move forward in a list, but there’s no easy way to move backwards.
● A doubly-linked list is a list where each cell stores two pointers: one to the next element in the list, and one to the previous element.
Marissa Megan Angela
![Page 144: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/144.jpg)
Doubly-Linked Lists
● In many cases, doubly-linked lists are similar to singly-linked lists.
● For example, if you’re just moving from the left to the right, then code on doubly-linked lists looks really similar to code on singly-linked lists.
Marissa Megan Angela
![Page 145: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/145.jpg)
Doubly-Linked Lists
● In many cases, doubly-linked lists are similar to singly-linked lists.
● For example, if you’re just moving from the left to the right, then code on doubly-linked lists looks really similar to code on singly-linked lists.
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next;
Cell* list = /* first cell */;list = list->next;
![Page 146: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/146.jpg)
Doubly-Linked Lists
● In many cases, doubly-linked lists are similar to singly-linked lists.
● For example, if you’re just moving from the left to the right, then code on doubly-linked lists looks really similar to code on singly-linked lists.
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next;
Cell* list = /* first cell */;list = list->next;
list
![Page 147: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/147.jpg)
Doubly-Linked Lists
● In many cases, doubly-linked lists are similar to singly-linked lists.
● For example, if you’re just moving from the left to the right, then code on doubly-linked lists looks really similar to code on singly-linked lists.
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next;
Cell* list = /* first cell */;list = list->next;
list
![Page 148: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/148.jpg)
Doubly-Linked Lists
● In many cases, doubly-linked lists are similar to singly-linked lists.
● For example, if you’re just moving from the left to the right, then code on doubly-linked lists looks really similar to code on singly-linked lists.
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next;
Cell* list = /* first cell */;list = list->next;
list
![Page 149: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/149.jpg)
Doubly-Linked Lists
● We can also move backwards in a doubly-linked list.
● Many algorithms are a lot easier to write if you can do this!
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next;
Cell* list = /* first cell */;list = list->next;
list
![Page 150: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/150.jpg)
Doubly-Linked Lists
● We can also move backwards in a doubly-linked list.
● Many algorithms are a lot easier to write if you can do this!
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next; list = list->prev;
Cell* list = /* first cell */;list = list->next; list = list->prev;
list
![Page 151: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/151.jpg)
Doubly-Linked Lists
● We can also move backwards in a doubly-linked list.
● Many algorithms are a lot easier to write if you can do this!
Marissa Megan Angela
Cell* list = /* first cell */;list = list->next; list = list->prev;
Cell* list = /* first cell */;list = list->next; list = list->prev;
list
![Page 152: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/152.jpg)
Doubly-Linked Lists
● It’s easy to remove a cell from a doubly-linked list: just wire the nodes next to it around it.
● (Don’t forget to handle edge cases!)
Marissa Megan Angela
cell
![Page 153: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/153.jpg)
Doubly-Linked Lists
● It’s easy to remove a cell from a doubly-linked list: just wire the nodes next to it around it.
● (Don’t forget to handle edge cases!)
Marissa Megan Angela
cell
![Page 154: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/154.jpg)
Doubly-Linked Lists
● It’s easy to remove a cell from a doubly-linked list: just wire the nodes next to it around it.
● (Don’t forget to handle edge cases!)
Marissa Megan Angela
cell delete
Dynamic
Deallocation!
![Page 155: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/155.jpg)
Doubly-Linked Lists
● It’s easy to remove a cell from a doubly-linked list: just wire the nodes next to it around it.
● (Don’t forget to handle edge cases!)
Marissa Angela
![Page 156: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/156.jpg)
For more on doubly-linked lists, check Section Handout 7 and Chapter 13 of
the textbook.
![Page 157: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/157.jpg)
To Recap
● If you want a function to change which object a pointer points to, pass that pointer in by reference.
● When passing in pointers by reference, make sure not to change the pointer unless you really mean it.
● Tail pointers make it easy to find the end of a linked list – a handy tool to keep in mind!
● Doubly-linked lists have each cell store pointers to both the next and previous cells in the list. They’re useful for when you need to remove out of a list.
![Page 158: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/158.jpg)
Your Action Items
● Read Chapter 13.● It’s all about different representations for
data and the relative tradeoffs. And there’s some great coverage of linked lists in there!
● Start Assignment 7.● It’s all about linked lists! Working through
this is a great
![Page 159: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b...Outline for Today Pointers by Reference Changing where you’re looking. Tail Pointers Speeding](https://reader034.vdocuments.us/reader034/viewer/2022051907/5ff9ae08f4e851715f02bdd0/html5/thumbnails/159.jpg)
Next Time
● Tree Structures● Representing branching structures in code.
● Binary Search Trees● Maintaining order at a low cost!