Linked Data Structures II: Doubly-Linked Lists 1 October 2020 OSU - - PowerPoint PPT Presentation

linked data structures ii doubly linked lists
SMART_READER_LITE
LIVE PREVIEW

Linked Data Structures II: Doubly-Linked Lists 1 October 2020 OSU - - PowerPoint PPT Presentation

Linked Data Structures II: Doubly-Linked Lists 1 October 2020 OSU CSE 1 Sequential Access Sequential access usually means accessing the entries of a collection (with a string model) in increasing order of position, by accessing the


slide-1
SLIDE 1

Linked Data Structures II: Doubly-Linked Lists

1 October 2020 OSU CSE 1

slide-2
SLIDE 2

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

1 October 2020 OSU CSE 2

slide-3
SLIDE 3

Interfaces and Classes

1 October 2020 OSU CSE 3

List implements implements ListKernel extends List1L List3 … Standard extends Iterable extends

slide-4
SLIDE 4

Interfaces and Classes

1 October 2020 OSU CSE 4

List implements implements ListKernel extends List1L List3 … Standard extends Iterable extends Standard has contracts for three methods: clear newInstance transferFrom

slide-5
SLIDE 5

Interfaces and Classes

1 October 2020 OSU CSE 5

List List1L List3 implements implements ListKernel extends … Standard extends Iterable extends ListKernel has contracts for six methods: addRightFront removeRightFront advance moveToStart leftLength rightLength

slide-6
SLIDE 6

Interfaces and Classes

1 October 2020 OSU CSE 6

List implements implements ListKernel extends List1L List3 … Standard extends Iterable extends List has contracts for fiveother methods: rightFront replaceRightFront moveToFinish retreat swapRights

slide-7
SLIDE 7

Mathematical Model

LIST_MODEL is ( left: string of T, right: string of T ) type ListKernel is modeled by LIST_MODEL

1 October 2020 OSU CSE 7

slide-8
SLIDE 8

Mathematical Model

LIST_MODEL is ( left: string of T, right: string of T ) type ListKernel is modeled by LIST_MODEL

1 October 2020 OSU CSE 8

You may think of these two strings as being to the left and right, respectively, of the “current position”.

slide-9
SLIDE 9

No-argument Constructor

  • Ensures:

this = (< >, < >)

1 October 2020 OSU CSE 9

slide-10
SLIDE 10

advance

void 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

1 October 2020 OSU CSE 10

slide-11
SLIDE 11

retreat

void 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

1 October 2020 OSU CSE 11

slide-12
SLIDE 12

What’s New?

  • With just advance, sequential access is
  • nly 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

1 October 2020 OSU CSE 12

slide-13
SLIDE 13

What’s New?

  • With just advance, sequential access is
  • nly 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

1 October 2020 OSU CSE 13

To see why, write an implementation of retreat using only the ListKernel methods.

slide-14
SLIDE 14

Example: List2 (SLL)

1 October 2020 OSU CSE 14

this = (<18>, <6>)

preStart ? data this lastLeft finish 1 leftLen 1 rightLen next 18 data 6 data next next

slide-15
SLIDE 15

Example: List2 (SLL)

1 October 2020 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) ...

slide-16
SLIDE 16

Example: List2 (SLL)

1 October 2020 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.

slide-17
SLIDE 17

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”

1 October 2020 OSU CSE 17

slide-18
SLIDE 18

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”

1 October 2020 OSU CSE 18

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

slide-19
SLIDE 19

Example: SLL “Bookends”

1 October 2020 OSU CSE 19

this = (<18>, <6>)

preStart ? data this lastLeft postFinish 1 leftLen 1 rightLen next 18 data ? data next next 6 data next ?

slide-20
SLIDE 20

Example: SLL “Bookends”

1 October 2020 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”.

slide-21
SLIDE 21

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 efficiently

1 October 2020 OSU CSE 21

slide-22
SLIDE 22

Example: List3 (DLL)

1 October 2020 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 ? ?

slide-23
SLIDE 23

Resources

  • Wikipedia: Linked Data Structure

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

  • Big Java (4th ed), Section 15.2 (but not the part

about iterators)

– https://library.ohio-state.edu/record=b8540788~S7

1 October 2020 OSU CSE 23