Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing...

162
Linked Lists Part Two

Transcript of Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing...

Page 1: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Linked ListsPart Two

Page 2: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Recap from Last Time

Page 3: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

...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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

New Stuff!

Page 6: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

A Problem

Page 7: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Why did this program crash?

Formulate a hypothesis, but don’t post anything in

chat just yet. 😃

Why did this program crash?

Formulate a hypothesis, but don’t post anything in

chat just yet. 😃

Page 8: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Why did this program crash?

Now, post your hypothesis in chat. Not sure? Just

answer with “??” 😃

Why did this program crash?

Now, post your hypothesis in chat. Not sure? Just

answer with “??” 😃

Page 9: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 10: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Processing Lists Iteratively

Page 11: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 12: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 13: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 14: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 15: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 16: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 17: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 18: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 20: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 21: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 26: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 37: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Printing a List

Page 39: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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

/* … other listy things. … */}

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

/* … other listy things. … */}

Page 40: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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

/* … other listy things. … */}

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

/* … other listy things. … */}

Page 41: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 42: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 44: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 45: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 59: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

What will happen if we reverse these two lines?

Formulate a hypothesis, but don’t post anything in

chat just yet. 😃

What will happen if we reverse these two lines?

Formulate a hypothesis, but don’t post anything in

chat just yet. 😃

Page 61: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

What will happen if we reverse these two lines?

Now, post your guess in chat. Not sure? Just answer

with “??” 😃

What will happen if we reverse these two lines?

Now, post your guess in chat. Not sure? Just answer

with “??” 😃

Page 62: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

Page 63: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Freeing a Linked List, Iteratively

Page 64: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

First, the Wrong Way

Page 65: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 66: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 67: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 68: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

delete

Dynamic

Deallocation!

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

Page 69: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list

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

Page 70: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list

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

Page 71: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list

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

Page 72: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

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

Page 73: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 74: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 75: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 76: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 77: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list

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

Page 78: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list next

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

Page 79: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!pudu!

list next

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

Page 80: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list next

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

Page 81: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list next

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

Page 82: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list next

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

Page 83: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list next

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

Page 84: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list

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

Page 85: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list

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

Page 86: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list

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

Page 87: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list next

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

Page 88: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

quokka! dikdik!

list next

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

Page 89: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list next

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

Page 90: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list next

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

Page 91: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list next

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

Page 92: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list next

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

Page 93: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list

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

Page 94: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list

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

Page 95: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list

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

Page 96: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list next

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

Page 97: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

dikdik!

list next

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

Page 98: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list next

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

Page 99: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list next

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

Page 100: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list next

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

Page 101: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list next

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

Page 102: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list

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

Page 103: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list

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

Page 104: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

list

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

Page 105: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 106: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Building a Linked List

Page 107: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 108: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 109: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 112: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 113: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 116: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 117: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 121: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 122: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 129: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 130: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 132: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 133: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 135: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 136: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 137: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 138: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 143: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 144: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 146: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 147: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 149: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 150: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 151: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 152: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 157: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 158: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 159: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 160: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

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 161: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Your Action Items

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

● Work on Assignment 7● Need help? Come talk to us! That’s what

we’re here for.

Page 162: Linked Lists · 2 days ago · Linked Lists 1 2 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the rest. The elements

Next Time

● Pointers By Reference● Combining two types of indirection!

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

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