118
Linked Lists Part Two

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

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A Quick Interlude for Announcements

Page 104: 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

Apply to Section Lead!

http://cs198.stanford.edu

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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!