linked data structures ii: doubly-linked...

23
Linked Data Structures II: Doubly-Linked Lists 20 February 2019 OSU CSE 1

Upload: others

Post on 18-Mar-2020

30 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Linked Data Structures II:Doubly-Linked Lists

20 February 2019 OSU CSE 1

Page 2: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Sequential Access• Sequential access usually means

accessing the entries of a collection (with a string model) in increasing order of position, by accessing the “next” entry in the collection

• Sometimes you can access the entries sequentially in the reverse direction, too, by accessing the “previous” entry in the collection– Example: the OSU CSE components List

20 February 2019 OSU CSE 2

Page 3: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Interfaces and Classes

20 February 2019 OSU CSE 3

Listimplements implements

ListKernel

extends

List1L List3…

Standard

extends

Iterable

extends

Page 4: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Interfaces and Classes

20 February 2019 OSU CSE 4

Listimplements implements

ListKernel

extends

List1L List3…

Standard

extends

Iterable

extendsStandard has contracts

for three methods:clear

newInstancetransferFrom

Page 5: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Interfaces and Classes

20 February 2019 OSU CSE 5

List

List1L List3

implements implements

ListKernel

extends

Standard

extends

Iterable

extendsListKernel has contracts for six

methods:addRightFront

removeRightFrontadvance

moveToStartleftLengthrightLength

Page 6: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Interfaces and Classes

20 February 2019 OSU CSE 6

Listimplements implements

ListKernel

extends

List1L List3…

Standard

extends

Iterable

extends

Listhas contracts for fiveother

methods:rightFront

replaceRightFrontmoveToFinish

retreatswapRights

Page 7: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Mathematical ModelLIST_MODEL is (left: string of T,

right: string of T)

type ListKernel is modeled by LIST_MODEL

20 February 2019 OSU CSE 7

Page 8: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Mathematical ModelLIST_MODEL is (left: string of T,

right: string of T)

type ListKernel is modeled by LIST_MODEL

20 February 2019 OSU CSE 8

You may think of these two strings as being to the left and right, respectively, of

the “current position”.

Page 9: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

No-argument Constructor

• Ensures:this = (< >, < >)

20 February 2019 OSU CSE 9

Page 10: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

advancevoid advance()

• Advances the position in this by one.• Updates: this• Requires:this.right /= < >

• Ensures:this.left * this.right =

#this.left * #this.right and|this.left| = |#this.left| + 1

20 February 2019 OSU CSE 10

Page 11: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

retreatvoid retreat()

• Retreats the position in this by one.• Updates: this• Requires:this.left /= < >

• Ensures:this.left * this.right =

#this.left * #this.right and|this.left| = |#this.left| - 1

20 February 2019 OSU CSE 11

Page 12: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

What’s New?

• With just advance, sequential access is only to the “next” position– A singly-linked list representation provides

good performance• With retreat as well as advance,

sequential access is also to the “previous” position– A singly-linked list representation provides

poor performance

20 February 2019 OSU CSE 12

Page 13: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

What’s New?

• With just advance, sequential access is only to the “next” position– A singly-linked list representation provides

good performance• With retreat as well as advance,

sequential access is also to the “previous” position– A singly-linked list representation provides

poor performance

20 February 2019 OSU CSE 13

To see why, write an implementation of

retreat using only the ListKernel methods.

Page 14: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Example: List2 (SLL)

20 February 2019 OSU CSE 14

this = (<18>, <6>)

preStart?

data

this

lastLeft

finish

1

leftLen

1

rightLen

next

18

data

6

data

next next

Page 15: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Example: List2 (SLL)

20 February 2019 OSU CSE 15

this = (<18>, <6>)

preStart?

data

this

lastLeft

finish

1

leftLen

1

rightLen

next

18

data

6

data

next next

The abstraction function (correspondence) ...

Page 16: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Example: List2 (SLL)

20 February 2019 OSU CSE 16

this = (<18>, <6>)

preStart?

data

this

lastLeft

finish

1

leftLen

1

rightLen

next

18

data

6

data

next next

The “current position” is indicated by this.lastLeft.

Page 17: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

A Second Smart Node

• Note that the code for Queue2 has no special cases at all, but the code for List2 needs to handle a special case in addRightFront and removeRightFront

• This can be eliminated by introducing a smart node at the end of the singly-linked list, too, so the two smart nodes are like “bookends”

20 February 2019 OSU CSE 17

Page 18: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

A Second Smart Node

• Note that the code for Queue2 has no special cases at all, but the code for List2 needs to handle a special case in addRightFront and removeRightFront

• This can be eliminated by introducing a smart node at the end of the singly-linked list, too, so the two smart nodes are like “bookends”

20 February 2019 OSU CSE 18

You should be able to re-write this code for List2 with two smart nodes, as illustrated on

the next slide.

Page 19: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Example: SLL “Bookends”

20 February 2019 OSU CSE 19

this = (<18>, <6>)

preStart?

data

this

lastLeft

postFinish

1

leftLen

1

rightLen

next

18

data

?

data

next next

6

data

next?

Page 20: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Example: SLL “Bookends”

20 February 2019 OSU CSE 20

this = (<18>, <6>)

preStart?

data

this

lastLeft

postFinish

1

leftLen

1

rightLen

next

18

data

?

data

next next

6

data

next?

There is really no need for a null reference any more;? here means “unused”.

Page 21: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Doubly-Linked Lists

• In addition to the second smart node, the code for List3 introduces one other (major) change

• The data structure is now a doubly-linked list, in which there are two references per node: one to the “next” node and one to the “previous” node– This allows retreat to be implemented

efficiently20 February 2019 OSU CSE 21

Page 22: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Example: List3 (DLL)

20 February 2019 OSU CSE 22

this = (<18>, <6>)

preStart?

data

this

lastLeft

postFinish

1

leftLen

1

rightLen

next

18

data

6

data

next next

prev prev prev

?

data

next

prev?

?

Page 23: Linked Data Structures II: Doubly-Linked Listsweb.cse.ohio-state.edu/.../2231/web-sw2/extras/slides/16.Linked-Data-Structures-II.pdfLinked Data Structures II: Doubly-Linked Lists

Resources• Wikipedia: Linked Data Structure

– http://en.wikipedia.org/wiki/Linked_data_structure

• Big Java, Section 15.2 (but not the part about iterators)– http://osu.worldcat.org/title/big-java/oclc/754642794

20 February 2019 OSU CSE 23