linked listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 ›...
TRANSCRIPT
![Page 1: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/1.jpg)
Linked ListsPart Two
![Page 2: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/2.jpg)
Recap from Last Time
![Page 3: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/3.jpg)
Linked Lists at a Glance
● 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.
1 2 3
![Page 4: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/4.jpg)
Linked Lists at a Glance
● 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.
1 2 3 4
![Page 5: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/5.jpg)
Linked Lists at a Glance
● 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.
1 2 3 4
![Page 6: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/6.jpg)
Linked Lists at a Glance
● 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.
1 2 3 4
![Page 7: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/7.jpg)
Linked Lists at a Glance
● 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.
1 2 3 4137
![Page 8: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/8.jpg)
Linked Lists at a Glance
● 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.
1 3 4137
![Page 9: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/9.jpg)
Linked List Cells
● A linked list is a chain of cells.● Each cell contains two pieces of
information:● Some piece of data that is stored in the
sequence, and● A link to the next cell in the list.
● We can traverse the list by starting at the first cell and repeatedly following its link.
![Page 10: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/10.jpg)
Representing a Cell
● For simplicity, let's assume we're building a linked list of strings.
● We can represent a cell in the linked list as a structure:
struct Cell { Type value; Cell* next; };
● The structure is defined recursively!
![Page 11: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/11.jpg)
Building a Linked List
![Page 12: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/12.jpg)
Cell* result = NULL;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 13: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/13.jpg)
Cell* result = NULL;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 14: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/14.jpg)
Cell* result = NULL;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 15: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/15.jpg)
Cell* result = NULL;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 16: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/16.jpg)
Cell* result = NULL;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 17: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/17.jpg)
Cell* result = NULL;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
dikdik!
line
![Page 18: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/18.jpg)
Cell* result = NULL;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
dikdik!
line
![Page 19: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/19.jpg)
Cell* result = NULL;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
dikdik!
line
![Page 20: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/20.jpg)
Cell* result = NULL;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
dikdik!
line
cell
![Page 21: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/21.jpg)
Cell* result = NULL;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
dikdik!
line
cell
???
![Page 22: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/22.jpg)
Cell* result = NULL;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
dikdik!
line
cell
???
![Page 23: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/23.jpg)
Cell* result = NULL;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
dikdik!
line
cell
???
![Page 24: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/24.jpg)
Cell* result = NULL;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
dikdik!
line
cell
dikdik!
???
![Page 25: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/25.jpg)
Cell* result = NULL;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
dikdik!
line
cell
dikdik!
???
![Page 26: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/26.jpg)
Cell* result = NULL;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
dikdik!
line
cell
dikdik!
![Page 27: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/27.jpg)
Cell* result = NULL;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
dikdik!
line
cell
dikdik!
![Page 28: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/28.jpg)
Cell* result = NULL;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
dikdik!
line
cell
dikdik!
![Page 29: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/29.jpg)
Cell* result = NULL;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
dikdik!
line
cell
dikdik!
![Page 30: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/30.jpg)
Cell* result = NULL;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
dikdik!
![Page 31: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/31.jpg)
Cell* result = NULL;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
dikdik!
![Page 32: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/32.jpg)
Cell* result = NULL;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
dikdik!
![Page 33: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/33.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
![Page 34: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/34.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
![Page 35: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/35.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
![Page 36: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/36.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
???
![Page 37: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/37.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
???
![Page 38: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/38.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
quokka!
???
![Page 39: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/39.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
quokka!
???
![Page 40: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/40.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
quokka!
![Page 41: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/41.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
quokka!
![Page 42: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/42.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
quokka!
![Page 43: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/43.jpg)
Cell* result = NULL;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
dikdik!
quokka!
line
cell
quokka!
![Page 44: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/44.jpg)
Cell* result = NULL;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
dikdik!quokka!
![Page 45: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/45.jpg)
Cell* result = NULL;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
dikdik!quokka!
![Page 46: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/46.jpg)
Cell* result = NULL;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
dikdik!quokka!
![Page 47: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/47.jpg)
Cell* result = NULL;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
dikdik!quokka!
line
![Page 48: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/48.jpg)
Cell* result = NULL;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
dikdik!quokka!
line
![Page 49: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/49.jpg)
Cell* result = NULL;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
dikdik!quokka!
line
![Page 50: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/50.jpg)
Cell* result = NULL;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
dikdik!quokka!
![Page 51: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/51.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
![Page 52: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/52.jpg)
Traversing a Linked List
1 2 4list 3
ptr
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
![Page 53: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/53.jpg)
Traversing a Linked List
1 2 4list 3
ptr
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
![Page 54: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/54.jpg)
Traversing a Linked List
1 2 4list 3
ptr
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
![Page 55: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/55.jpg)
Traversing a Linked List
1 2 4list 3
ptr
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
![Page 56: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/56.jpg)
Traversing a Linked List
1 2 4list 3
ptr
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
![Page 57: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/57.jpg)
Traversing a Linked List
1 2 4list 3
ptr
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
![Page 58: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/58.jpg)
Once More With Recursion
● Linked lists are defined recursively, and we can traverse them using recursion!
void recursiveTraverse(Cell* list) {
if (list == NULL) return;
/* … do something with list … */
recursiveTraverse(list->next);
}
![Page 59: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/59.jpg)
Freeing a Linked List
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● Here's an Extremely Bad Idea:for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
delete ptr;
}
![Page 60: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/60.jpg)
Freeing a Linked List
● All good things must come to an end, and we eventually need to reclaim the memory for a linked list.
● Here's an Extremely Bad Idea:for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
delete ptr;
}
![Page 61: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/61.jpg)
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.
● Here's an Extremely Bad Idea:for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
delete ptr;
}
![Page 62: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/62.jpg)
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.
● Here's an Extremely Bad Idea:for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
delete ptr;
}
![Page 63: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/63.jpg)
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.
● Here's an Extremely Bad Idea:for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
delete ptr;
}
![Page 64: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/64.jpg)
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 65: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/65.jpg)
Linked Lists: The Tricky Parts
● 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?
![Page 66: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/66.jpg)
What went wrong?
![Page 67: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/67.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
![Page 68: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/68.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
![Page 69: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/69.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
![Page 70: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/70.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
![Page 71: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/71.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list 137value
![Page 72: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/72.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list 137value
![Page 73: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/73.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list 137valuenewCell
![Page 74: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/74.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
???
list 137valuenewCell
![Page 75: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/75.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
???
list 137valuenewCell
![Page 76: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/76.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
137
???
list 137valuenewCell
![Page 77: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/77.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
137
???
137valuenewCell
![Page 78: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/78.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
137
137valuenewCell
![Page 79: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/79.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
137
137valuenewCell
![Page 80: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/80.jpg)
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
int main() { Cell* list = NULL; ListInsert(list, 137); ListInsert(list, 42); ListInsert(list, 271);}
list
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell* list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list
137
137valuenewCell
![Page 81: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/81.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
137
![Page 82: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/82.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
137
Why doesnobody love me?
![Page 83: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/83.jpg)
Pointers by Reference
● In order to resolve this problem, we must pass the linked list pointer by reference.
● Our new function:
void listInsert(Cell*& list, int value) {
Cell* newCell = new Cell;
cell->value = value;
cell->next = list;
list = cell;
}
![Page 84: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/84.jpg)
Pointers by Reference
● In order to resolve this problem, we must pass the linked list pointer by reference.
● Our new function:
void listInsert(Cell*& list, int value) {
Cell* newCell = new Cell;
cell->value = value;
cell->next = list;
list = cell;
}
![Page 85: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/85.jpg)
Pointers by Reference
● In order to resolve this problem, we must pass the linked list pointer by reference.
● Our new function:
void listInsert(Cell*& list, int value) {
Cell* newCell = new Cell;
cell->value = value;
cell->next = list;
list = cell;
}
This is a reference to a pointer to a Cell. It's often useful to read this from the
right to the left.
This is a reference to a pointer to a Cell. It's often useful to read this from the
right to the left.
![Page 86: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/86.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
![Page 87: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/87.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
![Page 88: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/88.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
![Page 89: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/89.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
![Page 90: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/90.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list 137value
![Page 91: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/91.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list 137value
![Page 92: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/92.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
list 137value newCell
![Page 93: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/93.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
???
list 137value newCell
![Page 94: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/94.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
???
???
list 137value newCell
![Page 95: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/95.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
137
???
list 137value newCell
![Page 96: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/96.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
137
???
list 137value newCell
![Page 97: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/97.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
137
list 137value newCell
![Page 98: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/98.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
137
list 137value newCell
![Page 99: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/99.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
void listInsert(Cell*& list, int value) { Cell* newCell = new Cell; newCell->value = value; newCell->next = list; list = newCell;}
137
list 137value newCell
![Page 100: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/100.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
137
![Page 101: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/101.jpg)
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
int main() { Cell* list = NULL; listInsert(list, 137); listInsert(list, 42); listInsert(list, 271);}
list
137
Yay! list loves me!
![Page 102: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/102.jpg)
Pointers by Reference
● If you pass a pointer into a function by value, you can change the contents at the object you point at, but not which object you point at.
● If you pass a pointer into a function by reference, you can also change which object is pointed at.
![Page 103: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/103.jpg)
A Quick Interlude for Announcements
![Page 105: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/105.jpg)
Announcements
● WiCS (Women in Computer Science) is holding a dessert night tonight!
● 6:30PM – 7:30PM at the Gates Patio.● RSVP:
https://docs.google.com/forms/d/12hcDPU9FGxSOMoTfehGSvT57yUw_4dtV32ezYPlfkk4/viewform
![Page 106: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/106.jpg)
Announcements
● Interested in applying engineering and technical skills to create social good? Attend the Stanford4Good Conference!
● This Saturday, 11AM – 3PM at the d.school.
● RSVP at http://bit.ly/Stanford4Good
![Page 107: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/107.jpg)
Back to our regularlyscheduled programming...
![Page 108: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/108.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
![Page 109: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/109.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
head
![Page 110: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/110.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
1head
![Page 111: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/111.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
1 2head
![Page 112: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/112.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
1 2 3head
![Page 113: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/113.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
2 3head
![Page 114: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/114.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
2 3 4head
![Page 115: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/115.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
3 4head
![Page 116: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/116.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
4head
![Page 117: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/117.jpg)
Implementing Queue
● Earlier, we implemented a queue using two stacks.
● The implementation supported enqueue and dequeue in average-case O(1).
● We can also implement a queue using linked lists!
● Idea:● To enqueue, append a new cell to the end of the
list.● To dequeue, remove the first cell from the list.
head
![Page 118: Linked Listsweb.stanford.edu › class › archive › cs › cs106b › cs106b.1136 › lectur… · A linked list is a data structure for storing a sequence of elements. Each element](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be09d7e708231d432a80b/html5/thumbnails/118.jpg)
A Surprising Fact
It is possible to build a Map that supports lookups that run, on average, in O(1) time.
How on earth is this possible?Find out on Friday!