159
Linked Lists Part Three

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

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Next Time

● Tree Structures● Representing branching structures in code.

● Binary Search Trees● Maintaining order at a low cost!