+ All Categories
Home > Documents > Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure...

Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure...

Date post: 03-Mar-2021
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
69
Linked Lists Part One
Transcript
Page 1: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Linked ListsPart One

Page 2: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Outline for Today

● Linked Lists, Conceptually● A different way to represent a sequence.

● Linked Lists, In Code● Some cool new C++ tricks.

Page 3: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Changing Offices

Page 4: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Dr. Cynthia Lee is no longer in room 100.

She can be found in room 108.

The Sign on Room 100

Room100

Room108

Page 5: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Dr. Cynthia Lee is no longer in room 108.

She can be found in room 190.

The Sign on Room 108

Room100

Room108

Room190

Page 6: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Dr. Cynthia Lee is no longer in room 190.

She can be found in room 192.

The Sign on Room 190

Room100

Room108

Room190

Room192

Page 7: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

The Sign on Room 192

Welcome to Cynthia’sOffice!

Room100

Room108

Room190

Room192

Page 8: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Linked Lists at a Glance

1 2 3

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

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

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

indicator.

Page 9: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Linked Lists at a Glance

1 2 3

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

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

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

indicator.

Page 10: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Linked Lists at a Glance

1 2 3137

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

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

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

indicator.

Page 11: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Linked Lists at a Glance

1 3137

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

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

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

indicator.

Page 12: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

...an empty list,or...

a single cell... ... that points at another linked list.

A Linked List is Either...

quokka! pudu! dikdik!kudu!

Page 13: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Representing Linked Lists

Page 14: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

...an empty list,or...

a single cell... ... that points at another linked list.

A Linked List is Either...

Page 15: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

...an empty list,or...

a single cell... ... that points at another linked list.

A Linked List is Either...

Page 16: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

a single cell... ... that points at another linked list.

struct Cell { string value; Cell* next;};

Hi Mom!

Page 17: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

struct Cell { string value; Cell* next;};

quokka! pudu! dikdik!kudu!

Page 18: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

struct Cell { string value; Cell* next;};

Cell* list = new Cell;

We just want a single cell, not an array of cells. To get the space we need, we’ll just say new Cell.

We just want a single cell, not an array of cells. To get the space we need, we’ll just say new Cell.

Notice that list is still a Cell*, a pointer to a cell.

It still says “look over there for your Cell”

rather than “I’m a Cell!”

Notice that list is still a Cell*, a pointer to a cell.

It still says “look over there for your Cell”

rather than “I’m a Cell!”

Yes, it’s confusing that C++ uses the same types to mean “look over there for an array

of Cells” and “look over there for a single Cell.”

Yes, it’s confusing that C++ uses the same types to mean “look over there for an array

of Cells” and “look over there for a single Cell.”

list

value

next

Page 19: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

struct Cell { string value; Cell* next;};

Cell* list = new Cell;list->value = "dikdik!";

Because list is a pointer to a Cell, we use the arrow operator -> instead of the

dot operator.

Think of list->value as saying “start at list,

follow an arrow, then pick the value field.”

Because list is a pointer to a Cell, we use the arrow operator -> instead of the

dot operator.

Think of list->value as saying “start at list,

follow an arrow, then pick the value field.”dikdik!

list

value

next

Page 20: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

struct Cell { string value; Cell* next;};

Cell* list = new Cell;list->value = "dikdik!";list->next = new Cell;list->next->value = "quokka!";list->next->next = new Cell;list->next->next->value = "pudu!";list->next->next->next = nullptr;

dikdik!

list

quokka! pudu!

value value value

next next next

Page 21: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

struct Cell { string value; Cell* next;}; Cell* list = new Cell;list->value = "dikdik!";list->next = new Cell;list->next->value = "quokka!";list->next->next = new Cell;list->next->next->value = "pudu!";list->next->next->next = nullptr;

dikdik!

list

quokka! pudu!

C++ uses the nullptr keyword to mean “a pointer

that doesn’t point at anything.”

(Older code uses NULL instead of nullptr; that’s also okay,

but we recommend nullptr.)

C++ uses the nullptr keyword to mean “a pointer

that doesn’t point at anything.”

(Older code uses NULL instead of nullptr; that’s also okay,

but we recommend nullptr.)

value value value

next next next

Page 22: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 23: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Measuring a Linked List

Page 24: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 25: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

dikdik! quokka! pudu!

Page 26: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Printing a Linked List

Page 27: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 28: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

dikdik! quokka! pudu!

Page 29: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

What happens if we switchthe order of these two lines?

Formulate a hypothesis,but don’t post anything

in chat just yet. 😃

What happens if we switchthe order of these two lines?

Formulate a hypothesis,but don’t post anything

in chat just yet. 😃

Page 30: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

What happens if we switchthe order of these two lines?

Now, post your hypothesisin chat. Not sure? Just

post “??.” 😃

What happens if we switchthe order of these two lines?

Now, post your hypothesisin chat. Not sure? Just

post “??.” 😃

Page 31: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

dikdik! quokka! pudu!

Page 32: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Time-Out for Announcements!

Page 33: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Assignment 7

● Assignment 6 was due today at 11:30AM.● Grace period for late submissions ends Sunday at

11:30AM Pacific time.● Assignment 7 (The Great Stanford Hash-Off)

goes out today. It’s due next Friday.● Implement linear probing and Robin Hood hashing!● See how fast these approaches are and how they

compare against chained hashing!● As always, come talk to us if you have any

questions! That’s what we’re here for.

Page 34: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

lecture = lecture->next;

Page 35: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Building a Linked List(without hardcoding it)

Page 36: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 37: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Cleaning Up a Linked List

Page 38: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Endearing C++ Quirks

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

delete[] ptr;

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

delete ptr;

● Make sure to use the proper deletion operation. Mixing these up is like walking off the end of an array or using an uninitialized pointer; it might work, or it might instantly crash your program, etc.

Page 39: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Cleaning Up Memory

● To free a linked list, we can’t just do this:

delete list;

● Why not?

Formulate a hypothesis,but don’t post anything

in chat just yet. 😃

Formulate a hypothesis,but don’t post anything

in chat just yet. 😃

Page 40: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Cleaning Up Memory

● To free a linked list, we can’t just do this:

delete list;

● Why not?

Now, post yourhypothesis in chat. 😃

Now, post yourhypothesis in chat. 😃

Page 41: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Cleaning Up Memory

● To free a linked list, we can’t just do this:

delete list;

● Why not?

list

Quokka PuduGerenuk

delete

Dynamic

Deallocation!

Page 42: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Cleaning Up Memory

● To free a linked list, we can’t just do this:

delete list;

● Why not?

list

Quokka Pudu

Page 43: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 44: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

What’s wrong with thiscode?

Formulate a hypothesis,but don’t post anything

in chat just yet. 😃

What’s wrong with thiscode?

Formulate a hypothesis,but don’t post anything

in chat just yet. 😃

Page 45: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

What’s wrong with thiscode?

Now, post your hypothesisin chat. Not sure? Just

post “??.” 😃

What’s wrong with thiscode?

Now, post your hypothesisin chat. Not sure? Just

post “??.” 😃

Page 46: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 47: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 48: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 49: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 50: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

Watch Out!

delete

Dynamic

Deallocation!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 51: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka Pudu

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 52: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka Pudu

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 53: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka Pudu

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);}

Page 54: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka Pudu

Watch Out!

void deleteList(Cell* list) { if (list == nullptr) return;

delete list; deleteList(list->next);} Undefined

behavior!

Page 55: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

In the Land of C++, wedo not speak to the dead.

What should we do instead?

Page 56: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

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

a single linked listcell that points...

... at another linked list.

A Linked List is Either...

Page 57: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 58: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 59: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 60: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Quokka PuduGerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 61: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Quokka PuduGerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

list

Page 62: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Quokka Pudu

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Gerenuk

list

Recursion!

Page 63: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Gerenuk

list

Page 64: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Gerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 65: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Gerenuk

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 66: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

Gerenuk

One Option

delete

Dynamic

Deallocation!

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 67: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

list

One Option

void deleteList(Cell* list) { if (list == nullptr) return;

deleteList(list->next); delete list;}

Page 68: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Your Action Items

● Read Chapter 12.1 – 12.3.● There’s lots of useful information in there

about how to work with linked lists.● Start Assignment 7

● Start working on linear probing. As always, come talk to us if you have any questions!

Page 69: Linked Lists · 2021. 2. 26. · Linked Lists at a Glance 1 137 3 A linked list is a data structure for storing a sequence of elements. Each element is stored separately from the

Next Time

● Linked Lists, Iteratively● How do you manually walk a linked list?

● Pointers Into Lists● Getting a helping hand.


Recommended