CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's...

34
CS 106B, Lecture 18 Linked Lists This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others

Transcript of CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's...

Page 1: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.

CS 106B, Lecture 18Linked Lists

This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others

Page 2: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

2

Plan for Today• Continuing discussion of ArrayStack from last week• Learn about a new way to store information: the linked list

Page 3: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

3

A Stack Class• Recall from last Monday our ArrayStack• By storing the array on the heap, the memory existed for all the

ArrayStack member functions

Page 4: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

4

Flaws with Arrays• Some adds are very costly (when we have to resize)

– Adding just one element requires copying all the elements• Imagine if everything were like that?

– Instead of just grabbing a new sheet of paper, re-copy all notes to a bigger sheet when you run out of space

• Idea: what if we could just add the amount of memory we need?

4 8 15 16 23 42

Page 5: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

5

Vector and arrays• Inserting into an array involves shifting all the elements over

– That's O(N)• What if we were able to easily insert?

4 8 15 23 41

16

Page 6: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

6

Linked List• Main idea: let's store every element in its own block of memory• Then we can just add one block of memory!• Then we can efficiently insert into the middle (or front)!• A Linked List is good for storing elements in an order (similar to

Vector)• Elements are chained together in a sequence• Each element is allocated on the heap

4 8 15 16 23 41

Page 7: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

7

Parts of a Linked List• What does each part of a Linked List need to store?

– element– pointer to the next element– We'll say the last node points to nullptr

• The ListNode struct:struct ListNode {

int data; // assume all elements are intsListNode *next;

// constructorListNode(int data, ListNode *next): data(data), next(next) {}// constructor without paramsListNode(): data(0), next(nullptr) {}

};4 8 15 16 23 41

Page 8: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

8

Creating a Linked ListListNode* front = new ListNode();

Page 9: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

9

Creating a Linked ListListNode* front = new ListNode();front->data = 42;

Page 10: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

10

Creating a Linked ListListNode* front = new ListNode();front->data = 42;front->next = new ListNode();

Page 11: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

11

Creating a Linked ListListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;

Page 12: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

12

Creating a Linked ListListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = new ListNode();

Page 13: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

13

Creating a Linked ListListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = new ListNode();front->next->next->data = 17;front->next->next->next = nullptr;

Page 14: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

14

Announcements• Assignment 5 is due on Thursday• Section attendance is mandatory• You can only partner with people in your section.

– If you partner with someone not in your section. It will result in a bucket grade deduction.

• Midterm/Regrade Requests– To check grade, go to gradescope.com and make an account. Use your

Stanford email for the account.

Page 15: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

15

Announcements• Assignment 5 is due on Thursday• Section attendance is mandatory• You can only partner with people in your section.

– If you partner with someone not in your section. It can result in a bucket grade deduction.

• Midterm/Regrade Requests

Page 16: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

16

Boolean Zen• Boolean Zen

if (functionCall() == true) {return true;

} else {return false;

}

Page 17: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

17

Boolean Zen• Better

if (functionCall()) {return true;

} else {return false;

}

Page 18: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

18

Boolean Zen• Best

return functionCall();

Page 19: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

19

Collections• Maps (and other ADTs)

string possibleKey = “Biden”;for (string mapKey : map) {

if (possibleKey == mapKey) {int value = map[possibleKey];

}}

• Review your ADTs! You need to be able to choose which ADT is appropriate and be able to use all of the ADTs we have learned.

Page 20: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

20

Assn. 5• The header file (.h file) is meant for others to peruse when using

your class. This is the interface. It should describe how to use the functions of your class, not how your class is implemented.

• The implementation file (.cpp file) should have commentsdescribing the implementation of your class. The users of your class typically wouldn’t look at the .cpp file.

Page 21: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

21

Linked List iteration• Idea: travel each ListNode one at a time

– No easy way to "index in" like with Vector. Why?• General syntax:

for (ListNode* ptr = list; ptr != nullptr; ptr = ptr->next) { /* … use ptr … */

}

Page 22: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

22

Linked List iteration• Idea: travel each ListNode one at a time

– No easy way to "index in" like with Vector. Why?• General syntax:

for (ListNode* ptr = list; ptr != nullptr; ptr = ptr->next) { /* … use ptr … */

}

Initialize ptr to the first node in (front node of) the list

Page 23: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

23

Linked List iteration• Idea: travel each ListNode one at a time

– No easy way to "index in" like with Vector. Why?• General syntax:

for (ListNode* ptr = list; ptr != nullptr; ptr = ptr->next) { /* … use ptr … */

}

Move ptr to point to the next node of the list

Page 24: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

24

Linked List iteration• Idea: travel each ListNode one at a time

– No easy way to "index in" like with Vector. Why?• General syntax:

for (ListNode* ptr = list; ptr != nullptr; ptr = ptr->next) { /* … use ptr … */

}

Continue doing this until we hit the end of the list

Page 25: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

25

Practice Iteratively!• Write a function that takes in the pointer to the front of a Linked List

and prints out all the elements of a Linked List

void printList(ListNode *front) {

}

Page 26: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

26

Practice Iteratively!• Write a function that takes in the pointer to the front of a Linked List

and prints out all the elements of a Linked List

void printList(ListNode *front) {for (ListNode* ptr = front; ptr != nullptr; ptr = ptr->next) {

cout << ptr->data << endl;}

}

Page 27: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

27

Alternative Iterationfor (ListNode* ptr = front; ptr != nullptr; ptr = ptr->next) {

// do something with ptr}

is equivalent to:

ListNode *ptr = front;while (ptr != nullptr) { // or while (ptr)

// do something with ptrptr = ptr->next;

}

Page 28: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

28

A Temporary SolutionWhat's wrong?

int main() {ListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = nullptr;while (front != nullptr) {cout << front->data << " ";front = front->next;

}// continue using frontreturn 0;

}

Page 29: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

29

A Temporary SolutionWhat's wrong?

int main() {ListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = nullptr;while (front != nullptr) {cout << front->data << " ";front = front->next;

}// continue using frontreturn 0;

}

Page 30: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

30

A Temporary SolutionWhat's wrong?

int main() {ListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = nullptr;while (front != nullptr) {cout << front->data << " ";front = front->next;

}// continue using frontreturn 0;

}

Page 31: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

31

A Temporary SolutionWhat's wrong?

int main() {ListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = nullptr;while (front != nullptr) {cout << front->data << " ";front = front->next;

}// continue using frontreturn 0;

}

Page 32: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

32

A Temporary SolutionWhat's wrong?

int main() {ListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = nullptr;while (front != nullptr) {cout << front->data << " ";front = front->next;

}// orphaned memory and empty list!return 0;

}

Page 33: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

33

Correct Versionint main() {ListNode* front = new ListNode();front->data = 42;front->next = new ListNode();front->next->data = -3;front->next->next = nullptr;ListNode *ptr = front;while (ptr != nullptr) {cout << ptr->data << " ";ptr = ptr->next;

}// front still has pointer to listreturn 0;

}

Page 34: CS 106B, Lecture 18 Linked Lists - Stanford University€¦ · Linked List •Main idea: let's store every element in its own block of memory •Then we can just add one block of

34

Overflow• From the book: 12.7 Copying Objects

– Shallow copying. C++ defaults to copy all instance variables. But if any are dynamically allocated, it will just copy the pointer (the address). Deep copying copies the underlying data as well.

– Two choices for user-defined classes: Either implement deep copying or forbid copying.