160
Linked Lists Part Two

Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Linked ListsPart Two

Page 2: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Recap from Last Time

Page 3: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Linked Lists

1 2 3137

● A linked list is a data structure for storing a sequence of elements.

● Each element is stored separately from the rest.● The elements are then chained together into a

sequence.● The end of the list is marked with some special

indicator.

Page 4: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

...an empty list,represented bynullptr, or...

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 5: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

New Stuff!

Page 6: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

A Problem

Page 7: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Stack Overflows

● Recursive code can result in stack overflows in cases where the recursion requires too many stack frames to finish a calculation.

● This means that recursion might not be the best strategy for manipulating linked lists, especially if those lists get really long.

● What should we do instead?

Page 8: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Processing Lists Iteratively

Page 9: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

Page 10: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

Page 11: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

quokka! dikdik!pudu!

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

list

Page 12: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

quokka! dikdik!pudu!

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

list

Page 13: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

list

quokka! dikdik!pudu!

Page 14: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

Page 15: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

Page 16: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

0

result

Page 17: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

0

result

Page 18: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

0

result

Page 19: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

1

result

Page 20: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

1

result

Page 21: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

1

result

Page 22: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

1

result

Page 23: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

1

result

Page 24: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

1

result

Page 25: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

2

result

Page 26: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

2

result

Page 27: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

2

result

Page 28: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

2

result

Page 29: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

2

result

Page 30: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

2

result

Page 31: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

3

result

Page 32: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

3

result

Page 33: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

3

result

Page 34: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

3

result

Page 35: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

3

result

Page 36: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

int lengthOf(Cell* list) { int result = 0; while (list != nullptr) { result++; list = list->next; } return result;}

list

3

result

Page 37: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

list

quokka! dikdik!pudu!

Page 38: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

int main() { Cell* list = readList(); cout << lengthOf(list) << endl;

/* … other listy things. … */}

list

quokka! dikdik!pudu!

Page 39: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Printing a List

Page 40: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

Page 41: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

Page 42: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

quokka! dikdik!pudu!

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

Page 43: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

quokka! dikdik!pudu!

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

Page 44: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

Page 45: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 46: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 47: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 48: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 49: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 50: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 51: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 52: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 53: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 54: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 55: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 56: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 57: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 58: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

void printList(Cell* list) { while (list != nullptr) { cout << list->value << endl; list = list->next; }}

list

Page 59: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

Page 60: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

list result

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

int main() { Cell* list = readList(); printList(list);

/* … other listy things. … */}

list

quokka! dikdik!pudu!

Page 61: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Freeing a Linked List, Iteratively

Page 62: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

First, the Wrong Way

Page 63: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG while (list != nullptr) { delete list; list = list->next; }} list

Page 64: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }} list

Page 65: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }} list

Page 66: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }} list

delete

Dynamic

Deallocation!

Page 67: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }} list

Page 68: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }} list

Page 69: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }}

void deleteList(Cell* list) { // WRONG WRONG WRONG WRONG // WRONG WRONG WRONG WRONG

while (list != nullptr) { delete list; list = list->next; }} list

Page 70: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) {

delete list; list = list->next; }}

void deleteList(Cell* list) { while (list != nullptr) {

delete list; list = list->next; }}

quokka! dikdik!pudu!

list

Page 71: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = list->next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = list->next; }}

list

Page 72: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 73: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 74: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 75: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 76: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 77: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!pudu!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 78: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 79: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 80: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 81: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 82: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 83: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 84: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 85: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 86: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

quokka! dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 87: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 88: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 89: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 90: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 91: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 92: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 93: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 94: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 95: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

dikdik!

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 96: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 97: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 98: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 99: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list next

Page 100: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 101: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 102: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

void deleteList(Cell* list) { while (list != nullptr) { Cell* next = list->next; delete list; list = next; }}

list

Page 103: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Pointers Into Lists

● When processing linked lists iteratively, it’s common to introduce pointers that point to cells in multiple spots in the list.

● This is particularly useful if we’re destroying or rewiring existing lists.

quokka! dikdik!pudu!

list next

Page 104: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Building a Linked List

Page 105: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

Page 106: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

Page 107: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

result

Page 108: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

result

Page 109: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

result

Page 110: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

result

Page 111: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

result

Page 112: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

result

Page 113: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

result

Page 114: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

result

Page 115: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell result

Page 116: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

???

result

Page 117: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

???

result

Page 118: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

???

result

Page 119: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

???

result

Page 120: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

???

result

Page 121: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

???

result

Page 122: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

???

result

Page 123: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

result

Page 124: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

result

Page 125: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

result

Page 126: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

result

Page 127: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!line

cell

dikdik!

result

Page 128: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!

result

Page 129: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!

result

Page 130: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

dikdik!

result

Page 131: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

dikdik!

result

Page 132: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

dikdik!

result

Page 133: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

dikdik!

result

Page 134: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

???

dikdik!

result

Page 135: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

???

dikdik!

result

Page 136: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

quokka!

???

dikdik!

result

Page 137: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

quokka!

???

dikdik!

result

Page 138: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

quokka! dikdik!

result

Page 139: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

quokka! dikdik!

result

Page 140: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

quokka! dikdik!

result

Page 141: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka!line

cell

quokka! dikdik!

result

Page 142: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka! dikdik!

result

Page 143: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka! dikdik!

result

Page 144: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka! dikdik!

result

Page 145: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

result

Page 146: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

result

Page 147: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

result

Page 148: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

???

Page 149: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

???

Page 150: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

pudu!

???

Page 151: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

pudu!

???

Page 152: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

pudu!

Page 153: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

pudu!

Page 154: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

pudu!

Page 155: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

resultcell

pudu!

Page 156: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

pudu!

line

quokka! dikdik!

result

pudu!

Page 157: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka! dikdik!

result

pudu!

It’s a bug: these elements are in the wrong order!

It’s a feature: we just implemented a stack using

linked lists!

It’s a bug: these elements are in the wrong order!

It’s a feature: we just implemented a stack using

linked lists!

Page 158: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Cell* result = nullptr;while (true) {

string line = getLine("Next entry? ");if (line == "") break;

Cell* cell = new Cell;cell->value = line;

cell->next = result;result = cell;

}return result;

quokka! dikdik!

result

pudu!

It’s a bug: these elements are in the wrong order!

It’s a feature: we just implemented a stack using

linked lists!

It’s a bug: these elements are in the wrong order!

It’s a feature: we just implemented a stack using

linked lists!

Page 159: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Your Action Items

● Read Chapter 12.1 – 12.3.● It’s a good overview of linked lists.

● Finish Assignment 6.● Need help? Come talk to us! That’s what

we’re here for.

Page 160: Linked Lists - web.stanford.edu€¦ · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest

Next Time

● Tail Pointers● Tracking the start and end of a list.

● Implementing the Queue● A Tale of Two Implementations.

● Variations on Linked Lists● What linked lists look like “in the wild.”