linked lists - stanford university€¦ · linked lists 1 2 137 3 a linked list is a data structure...

136
Linked Lists Part Two

Upload: others

Post on 29-May-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

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

Linked ListsPart Two

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

Recap from Last Time

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

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

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

struct Cell { Type data; Cell* next;};

struct Cell { Type data; Cell* next;};

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

Processing Lists Recursively

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

Processing Lists Iteratively

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

Linked Lists, Iteratively

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

1 2 43list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

Linked Lists, Iteratively

1 2 43

curr

● You can navigate a linked list using a traditional for loop:for (Cell* curr = list; curr != nullptr; curr = curr->next) {

/* … do something with curr->value … */

}

list

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

New Stuff!

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

ap·op·to·sis

the death of cells whichoccurs as a normaland controlled part

of an organism's growthor development.

Source: Google

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

Endearing C++ Quirks

● If you allocate memory using the new[] operator (e.g. new int[137]), you have to free it using the delete[] operator.

delete[] ptr;

● If you allocate memory using the new operator (e.g. new Cell), you have to free it using the delete operator.

delete ptr;

● Make sure to use the proper deletion operation. Mixing these up leads to Undefined Behavior.

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

Freeing a Linked List

● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.

● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {

delete ptr;

}

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

Freeing a Linked List

● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.

● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {

delete ptr;

}

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

Freeing a Linked List

2 ...ptr

● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.

● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {

delete ptr;

}

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

Freeing a Linked List

2 ...ptr

● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.

● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {

delete ptr;

}

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

Freeing a Linked List

???ptr

● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.

● The following code triggers undefined behavior. Don’t do this!for (Cell* ptr = list; ptr != nullptr; ptr = ptr->next) {

delete ptr;

}

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

Freeing a Linked List Properly

● To properly free a linked list, we have to be able to● destroy a cell, and● advance to the cell after it.

● How might we accomplish this?

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

pudu!

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

pudu!

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

pudu!

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

pudu!

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

pudu!

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

pudu!

next

poof!

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

???

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

???

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

quokka! dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

???dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

???dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

dikdik!

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

???

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

???

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

list

next

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

list

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

while (list != nullptr) {Cell* next = list->next;

delete list; list = next;}

list

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

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

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

Pointers and References

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

Prepending an Element

● Suppose that we want to write a function that will add an element to the front of a linked list.

● What might this function look like?

sunfish elephantwhale

list

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

Prepending an Element

● Suppose that we want to write a function that will add an element to the front of a linked list.

● What might this function look like?

arapaima sunfish elephantwhale

list

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

Prepending an Element

● Suppose that we want to write a function that will add an element to the front of a linked list.

● What might this function look like?

arapaima sunfish elephantwhale

list

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

What went wrong?

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

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

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

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

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

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

list

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

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

list

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list Sartrevalue

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list Sartrevalue

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

???

list SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

???

list SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

Sartre

???

list SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list

Sartre

???

SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list

Sartre

SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list

Sartre

SartrevaluenewCell

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

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = NULL; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell* list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

list

Sartre

SartrevaluenewCell

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

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

list

Sartre

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

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "Sartre"); listInsert(list, "Camus"); listInsert(list, "Nietzsche");

return 0;}

list

Sartre

Hell is other pointers

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

Pointers by Reference

● To resolve this problem, we can pass the linked list pointer by reference.

● Our new function:

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

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

Pointers by Reference

● To resolve this problem, we can pass the linked list pointer by reference.

● Our new function:

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

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

Pointers by Reference

● To resolve this problem, we can pass the linked list pointer by reference.

● Our new function:

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;} This is a reference to a

pointer to a Cell. If we change where list points in this function,

the changes will stick!

This is a reference to a pointer to a Cell. If we change where list points in this function,

the changes will stick!

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

listThe

Turtlesvalue

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

listThe

Turtlesvalue

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

???

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

???

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

TheTurtles

???

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

TheTurtles

???

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

TheTurtles

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

TheTurtles

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "A"); listInsert(list, "B"); listInsert(list, "C");

return 0;}

list

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

void listInsert(Cell*& list, const string& value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}

TheTurtles

listThe

Turtlesvalue newCell

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

TheTurtles

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

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

int main() { Cell* list = nullptr; listInsert(list, "The Turtles"); listInsert(list, "The Beatles"); listInsert(list, "A Flock of Seagulls");

return 0;}

list

TheTurtles

🎶 So happy 🎶 🎶 together 🎶

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

Pointers by Reference

● If you pass a pointer into a function by value, you can change the contents at the object you point at, but not which object you point at.

● If you pass a pointer into a function by reference, you can also change which object is pointed at.

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

Time-Out for Announcements!

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

Midterm Timetable

● You’re done with the midterm exam! Woohoo!

● We’ll be grading it over the weekend and returning graded exams on Monday along with stats and solutions.

● Have any questions in the meantime? Just ask!

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

Assignment 5

● Assignment 5 (Data Sagas) is due one week from today.

● We assume most of you have not yet started, and that’s fine. Start working through that assignment this evening and make slow and steady progress.

● Have questions? Stop by the LaIR or CLaIR!

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

lecture = announcements->next;

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

Implementing the Queue

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

gerenuk

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

gerenuk impala

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

gerenuk impala greaterkudu

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

gerenuk impala greaterkudu

alpaca

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

gerenuk impala greaterkudu

alpaca slowloris

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

impala greaterkudu

alpaca slowloris

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

Implementing the Queue

● There are many ways to implement the Queue, and a common one is to use linked lists.● New elements get added to the back of the list.● Dequeued elements are taken off the front of

the list.● Question: How efficient is this?

greaterkudu

alpaca slowloris

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

1 2 3

head

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

1 2 3

head tail

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

1 2 3

head tail

4

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

1 2 3

head tail

4

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

2 3

head tail

4

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

3

head tail

4

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

head tail

4

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

Tail Pointers

● A tail pointer is a pointer to the last element of a linked list.

● Tail pointers make it easy and efficient to add new elements to the back of a linked list.

● We can use tail pointers to implement an efficient Queue using a linked list.

head tail

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

class OurQueue {public: OurQueue(); ~OurQueue();

int peek() const; void enqueue(int value); int dequeue();

int size() const; bool isEmpty() const;

private: struct Cell { int value; Cell* next; };

Cell* head; Cell* tail;};

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

Enqueuing Things

● Case 1: The queue is empty.

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

Enqueuing Things

● Case 1: The queue is empty.

head tail

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

Enqueuing Things

● Case 1: The queue is empty.

1

head tail

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

Enqueuing Things

● Case 1: The queue is empty.

head tail

1

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

Enqueuing Things

● Case 1: The queue is empty.

● Case 2: The queue is not empty.

head tail

1 3 7

head tail

1

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

Enqueuing Things

● Case 1: The queue is empty.

● Case 2: The queue is not empty.

head tail

1 3 7

head tail

1

137

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

Enqueuing Things

● Case 1: The queue is empty.

● Case 2: The queue is not empty.

head tail

1 3 7

head tail

1

137

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

Dequeuing Things

● Case 1: Dequeuing when there are 2+ elements.

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

Dequeuing Things

● Case 1: Dequeuing when there are 2+ elements.

head tail

1 3 7 137

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

Dequeuing Things

● Case 1: Dequeuing when there are 2+ elements.

head tail

1 3 7 137

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

Dequeuing Things

● Case 1: Dequeuing when there are 2+ elements.

head tail

3 7 137

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

Dequeuing Things

● Case 1: Dequeuing when there are 2+ elements.

Case 2: Dequeuing the last element.

head tail

3 7 137

head tail

137

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

Dequeuing Things

● Case 1: Dequeuing when there are 2+ elements.

Case 2: Dequeuing the last element.

head tail

3 7 137

head tail

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

The Overall Analysis

● Implementing a queue using a linked list without a tail pointer:● Cost of an enqueue: O(n)● Cost of a dequeue: O(1)

● Implementing a queue using a linked list with a tail pointer:● Cost of an enqueue: O(1)● Cost of a dequeue: O(1)

● This is really, really fast!

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

Your Action Items

● Read Chapter 12 of the textbook (and, optionally, Chapter 13).● It’ll provide more information about linked

lists, data structure implementation, and runtime efficiency.

● Work on Assignment 5.● At a bare minimum, read through the handout

and make sure you know what’s asked of you.● Recommendation: Complete multiway merge

and start lower bound searching by next time.

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

Next Time

● Tree Structures● Encoding trees directly in software!

● Binary Search Trees● A fast, flexible, powerful data structure.