lecture 10: linked listscs61a/su16/assets/slides/10-linked_lists_full.pdf•another way to implement...

124
Brian Hou July 6, 2016 Lecture 10: Linked Lists

Upload: others

Post on 24-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Brian Hou July 6, 2016

Lecture 10: Linked Lists

Page 2: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

Page 3: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)

Page 4: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests

Page 5: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org

Page 6: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

Page 7: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 4 is due 7/7

Page 8: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 4 is due 7/7• Quiz 3 is tomorrow at the beginning of lecture

Page 9: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 4 is due 7/7• Quiz 3 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

Page 10: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 4 is due 7/7• Quiz 3 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

• Quiz 4 will be released 9 am on 7/11, due 10 am on 7/12

Page 11: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 4 is due 7/7• Quiz 3 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

• Quiz 4 will be released 9 am on 7/11, due 10 am on 7/12• 61A Potluck on 7/8! 5 - 8 pm (or later) in Wozniak Lounge

Page 12: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Announcements

• Project 2 is due 7/12 (+1 EC point if submitted 7/12)• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 4 is due 7/7• Quiz 3 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

• Quiz 4 will be released 9 am on 7/11, due 10 am on 7/12• 61A Potluck on 7/8! 5 - 8 pm (or later) in Wozniak Lounge

• Bring food and board games!

Page 13: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

Page 14: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants

Page 15: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day

Page 16: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

Page 17: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

Page 18: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

Page 19: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

1.Going Deep Blue

Page 20: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

1.Going Deep Blue

1.The best team on the3rd floor of Davidson (U2)

Page 21: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

1.Going Deep Blue

1.The best team on the3rd floor of Davidson (U2)

1.Going DeepMind

Page 22: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

1.Going Deep Blue

1.The best team on the3rd floor of Davidson (U2)

1.Going DeepMind

Page 23: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

1.Going Deep Blue

1.The best team on the3rd floor of Davidson (U2)

1.Going DeepMind

Thank you to all the participants!

Page 24: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Hog Contest

• 76 contestants• 20 new challengers on the last day• 11 new challengers in the last 6 hours

• The winner:

1.Edgar Orendain

1.Going Deep Blue

1.The best team on the3rd floor of Davidson (U2)

1.Going DeepMind

Thank you to all the participants!

Full rankings: cs61a.org/proj/hog_contest

Page 25: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Roadmap

Introduction

Functions

Data

Mutability

Objects

Interpretation

Paradigms

Applications

Page 26: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Roadmap

Introduction

Functions

Data

Mutability

Objects

Interpretation

Paradigms

Applications

• This week (Data), the goals are:

Page 27: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Roadmap

Introduction

Functions

Data

Mutability

Objects

Interpretation

Paradigms

Applications

• This week (Data), the goals are:• To continue our journey through

abstraction with data abstraction

Page 28: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Roadmap

Introduction

Functions

Data

Mutability

Objects

Interpretation

Paradigms

Applications

• This week (Data), the goals are:• To continue our journey through

abstraction with data abstraction• To study useful data types we can

construct with data abstraction

Page 29: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

Page 30: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

Page 31: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

• Great programmers use data abstraction to separate:

Page 32: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

• Great programmers use data abstraction to separate:• How compound values are used (the unit)

Page 33: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

• Great programmers use data abstraction to separate:• How compound values are used (the unit)

• How compound values are represented (the parts)

Page 34: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

• Great programmers use data abstraction to separate:• How compound values are used (the unit)

• How compound values are represented (the parts)

Abstraction Barrier

Page 35: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Data Abstraction

• Great programmers use data abstraction to separate:• How compound values are used (the unit)

• How compound values are represented (the parts)

Abstraction Barrier

Constructors and Selectors

Page 36: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Abstraction Barrier Violations

Page 37: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Abstraction Barrier Violations

• Constructors and selectors provide us with abstraction, allowing us to use the data type without having to know its implementation

Page 38: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Abstraction Barrier Violations

• Constructors and selectors provide us with abstraction, allowing us to use the data type without having to know its implementation

• An abstraction barrier violation is when we assume knowledge about the data type implementation, rather than using constructors and selectors

Page 39: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Abstraction Barrier Violations

• Constructors and selectors provide us with abstraction, allowing us to use the data type without having to know its implementation

• An abstraction barrier violation is when we assume knowledge about the data type implementation, rather than using constructors and selectors

Never violate the abstraction barrier!

Page 40: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Sequences

Page 41: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

The Sequence Abstraction

Page 42: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

The Sequence Abstraction

The sequence abstraction is a collection of behaviors:

Page 43: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

The Sequence Abstraction

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0.

The sequence abstraction is a collection of behaviors:

Page 44: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

The Sequence Abstraction

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0.

The sequence abstraction is a collection of behaviors:

Lists and strings are both examples of sequences.

Page 45: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

The Sequence Abstraction

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0.

The sequence abstraction is a collection of behaviors:

Lists and strings are both examples of sequences.

We can use built-in syntax associated with this behavior. We can also use functions.

Page 46: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

The Sequence Abstraction

Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non-negative integer index less than its length, starting at 0.

The sequence abstraction is a collection of behaviors:

Lists and strings are both examples of sequences.

We can use built-in syntax associated with this behavior. We can also use functions.

(demo)

Page 47: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists

Page 48: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists

Page 49: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction

Linked Lists

Page 50: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

Linked Lists

Page 51: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link

Linked Lists

Page 52: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

Linked Lists

Page 53: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

Page 54: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

1

Page 55: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

1 2

Page 56: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

1 2 3

Page 57: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

1 2 3

Page 58: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

1 2 3

Page 59: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

Linked Lists

1 2 3

Page 60: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

• This data structure has many names:

Linked Lists

1 2 3

Page 61: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

• This data structure has many names:• Linked list (C, Java)• List (Lisp)• Forward list (C++)

Linked Lists

1 2 3

Page 62: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

• Another way to implement the sequence abstraction• Links have two parts

• first: the element in the link• rest: the next link in the list

• This is a recursive definition: the rest of a linked list is another linked list

• This data structure has many names:• Linked list (C, Java)• List (Lisp)• Forward list (C++)• Linky Listys (TAs)

Linked Lists

1 2 3

Page 63: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked List Abstraction

Page 64: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Linked List Abstraction

Page 65: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def first(s): """Return the first element of a linked list S."""def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Linked List Abstraction

Page 66: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def first(s): """Return the first element of a linked list S."""def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Linked List Abstraction

If a linked list s is constructed from a first element h and a linked list t, then

Page 67: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def first(s): """Return the first element of a linked list S."""def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Linked List Abstraction

If a linked list s is constructed from a first element h and a linked list t, then • first(s) returns h, which is an element of the sequence

Page 68: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def first(s): """Return the first element of a linked list S."""def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Linked List Abstraction

If a linked list s is constructed from a first element h and a linked list t, then • first(s) returns h, which is an element of the sequence• rest(s) returns t, which is a linked list

Page 69: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

Page 70: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Page 71: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

def first(s): """Return the first element of a linked list S."""

def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list."""

Page 72: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

def first(s): """Return the first element of a linked list S."""

def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list.""" return [first, rest]

Page 73: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

def first(s): """Return the first element of a linked list S.""" return s[0]

def rest(s): """Return the rest of the elements of a linked list S."""

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list.""" return [first, rest]

Page 74: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

def first(s): """Return the first element of a linked list S.""" return s[0]

def rest(s): """Return the rest of the elements of a linked list S.""" return s[1]

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list.""" return [first, rest]

Page 75: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1) (demo)

def first(s): """Return the first element of a linked list S.""" return s[0]

def rest(s): """Return the rest of the elements of a linked list S.""" return s[1]

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list.""" return [first, rest]

Page 76: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists are Sequences

Page 77: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists are Sequences (demo)

Page 78: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def len_link(s): """Return the length of the linked list.""" length = 0 while s != empty: s, length = rest(s), length + 1 return length

Linked Lists are Sequences (demo)

Page 79: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def getitem_link(s, i): """Return the element at index i.""" while i > 0: s, i = rest(s), i - 1 return first(s)

def len_link(s): """Return the length of the linked list.""" length = 0 while s != empty: s, length = rest(s), length + 1 return length

Linked Lists are Sequences (demo)

Page 80: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def getitem_link(s, i): """Return the element at index i.""" while i > 0: s, i = rest(s), i - 1 return first(s)

def len_link(s): """Return the length of the linked list.""" length = 0 while s != empty: s, length = rest(s), length + 1 return length

Linked Lists are Sequences

Never violate the abstraction barrier!

(demo)

Page 81: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists are Recursive

Page 82: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists are Recursive (demo)

Page 83: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def len_link(s): """Return the length of the linked list.""" if s == empty: return 0 else: return 1 + len_link(rest(s))

Linked Lists are Recursive (demo)

Page 84: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def getitem_link(s, i): """Return the element at index i.""" if i == 0: return first(s) else: return getitem_link(rest(s), i - 1)

def len_link(s): """Return the length of the linked list.""" if s == empty: return 0 else: return 1 + len_link(rest(s))

Linked Lists are Recursive (demo)

Page 85: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def getitem_link(s, i): """Return the element at index i.""" if i == 0: return first(s) else: return getitem_link(rest(s), i - 1)

def len_link(s): """Return the length of the linked list.""" if s == empty: return 0 else: return 1 + len_link(rest(s))

Linked Lists are Recursive (demo)

Never violate the abstraction barrier!

Page 86: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Break!

Page 87: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked List Processing

Page 88: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Sequences as Containers

Page 89: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Sequences as Containers (demo)

Page 90: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Sequences as Containers (demo)

def contains(s, elem): """Return whether ELEM is in the sequence S. >>> contains([1, 2, 3], 1) True >>> contains([1, 2, 3], 4) False """ for x in s: if x == elem: return True return False

Page 91: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists as Containers (demo)

Page 92: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked Lists as Containers (demo)

def contains_link(s, elem): """Return whether ELEM is in the sequence S. >>> contains_link(link(1, link(2, link(3, empty))), 1) True >>> contains_link(link(1, link(2, link(3, empty))), 4) False """ if s == empty: return False if first(s) == elem: return True else: return contains(rest(s), elem)

Page 93: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Linked List Examples

Page 94: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Counting Partitions

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0: return 0 else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 95: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

Page 96: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions (demo)

Page 97: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m):

(demo)

Page 98: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0:

(demo)

Page 99: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty)

(demo)

Page 100: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0:

(demo)

Page 101: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty

(demo)

Page 102: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty else:

(demo)

Page 103: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty else: with_m = partitions(n-m, m)

(demo)

Page 104: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty else: with_m = partitions(n-m, m) without_m = partitions(n, m-1)

(demo)

Page 105: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty else: with_m = partitions(n-m, m) without_m = partitions(n, m-1) add_m = lambda s: link(m, s)

(demo)

Page 106: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty else: with_m = partitions(n-m, m) without_m = partitions(n, m-1) add_m = lambda s: link(m, s) with_m = map_link(add_m, with_m)

(demo)

Page 107: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Enumerating Partitions

def partitions(n, m): if n == 0: return link(empty, empty) elif n < 0 or m == 0: return empty else: with_m = partitions(n-m, m) without_m = partitions(n, m-1) add_m = lambda s: link(m, s) with_m = map_link(add_m, with_m) return extend(with_m, without_m)

(demo)

Page 108: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Other Linked List Implementations

Page 109: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v1)

def first(s): """Return the first element of a linked list S.""" return s[0]

def rest(s): """Return the rest of the elements of a linked list S.""" return s[1]

def link(first, rest): """Construct a linked list from its first element and the rest of the linked list.""" return [first, rest]

Page 110: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Implementing Linked Lists (v2)

Page 111: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest):

Implementing Linked Lists (v2)

Page 112: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest):

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 113: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg):

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 114: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first':

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 115: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 116: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest':

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 117: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest': return rest

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 118: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest': return rest return dispatch

def first(s):

Implementing Linked Lists (v2)

def rest(s):

Page 119: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest': return rest return dispatch

def first(s): return s('first')

Implementing Linked Lists (v2)

def rest(s):

Page 120: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest': return rest return dispatch

def first(s): return s('first')

Implementing Linked Lists (v2)

def rest(s): return s('rest')

Page 121: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest': return rest return dispatch

def first(s): return s('first')

Implementing Linked Lists (v2) (demo)

def rest(s): return s('rest')

Page 122: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'first': return first elif msg == 'rest': return rest return dispatch

def first(s): return s('first')

Implementing Linked Lists (v2) (demo)

def rest(s): return s('rest')

Page 123: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

def link(first, rest): def dispatch(msg): if msg == 'brian': return first elif msg == 'marvin': return rest return dispatch

def first(s): return s('brian')

Implementing Linked Lists (v3)

def rest(s): return s('marvin')

Page 124: Lecture 10: Linked Listscs61a/su16/assets/slides/10-Linked_Lists_full.pdf•Another way to implement the sequence abstraction • Links have two parts • first: the element in the

Summary

• Linked lists are one implementation of the sequence abstraction

• Linked lists are composed of two parts: • first: the element in the link • rest: the next link in the list (may be empty)

• Data abstraction means that the implementation details of the first and rest selectors are unnecessary

• We can use functions to implement linked lists • We can use lists to implement dictionaries • Therefore, we can use functions to implement

dictionaries