linked data structures ii doubly linked lists
play

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


  1. Linked Data Structures II: Doubly-Linked Lists 1 October 2020 OSU CSE 1

  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

  3. Interfaces and Classes Standard Iterable extends extends ListKernel extends List implements implements … List1L List3 1 October 2020 OSU CSE 3

  4. Interfaces and Classes Standard Iterable extends extends Standard has contracts ListKernel for three methods: clear newInstance extends transferFrom List implements implements … List1L List3 1 October 2020 OSU CSE 4

  5. Interfaces and Classes Standard Iterable ListKernel has contracts for six extends extends methods: addRightFront ListKernel removeRightFront advance extends moveToStart leftLength List rightLength implements implements … List1L List3 1 October 2020 OSU CSE 5

  6. Interfaces and Classes List has contracts for fiveother Standard Iterable methods: rightFront extends extends replaceRightFront moveToFinish retreat ListKernel swapRights extends List implements implements … List1L List3 1 October 2020 OSU CSE 6

  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

  8. Mathematical Model LIST_MODEL is ( You may think of these two left: string of T, strings as being to the left and right, respectively, of right: string of T the “current position”. ) type ListKernel is modeled by LIST_MODEL 1 October 2020 OSU CSE 8

  9. No-argument Constructor • Ensures: this = (< >, < >) 1 October 2020 OSU CSE 9

  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

  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

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

  13. What’s New? To see why, write an • With just advance , sequential access is implementation of only to the “next” position retreat using only the ListKernel methods. – 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

  14. Example: List2 (SLL) this = (<18>, <6>) ? 18 6 preStart data data data lastLeft next next next this finish 1 leftLen 1 rightLen 1 October 2020 OSU CSE 14

  15. Example: List2 (SLL) this = (<18>, <6>) ? 18 6 preStart data data data lastLeft next next next this finish 1 leftLen The abstraction function 1 (correspondence) ... rightLen 1 October 2020 OSU CSE 15

  16. Example: List2 (SLL) this = (<18>, <6>) ? 18 6 preStart data data data lastLeft next next next this finish 1 leftLen The “current position” is 1 indicated by this .lastLeft . rightLen 1 October 2020 OSU CSE 16

  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

  18. A Second Smart Node You should be able to re-write this code for List2 with two smart nodes, as illustrated on • Note that the code for Queue2 has no the next slide. 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

  19. Example: SLL “Bookends” this = (<18>, <6>) ? 18 6 ? preStart data data data data ? lastLeft next next next next this postFinish 1 leftLen 1 rightLen 1 October 2020 OSU CSE 19

  20. Example: SLL “Bookends” this = (<18>, <6>) ? 18 6 ? preStart data data data data ? lastLeft next next next next this postFinish 1 There is really no need for a leftLen null reference any more; 1 ? here means “unused”. rightLen 1 October 2020 OSU CSE 20

  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

  22. Example: List3 (DLL) this = (<18>, <6>) ? ? 18 6 preStart data data data data ? lastLeft next next next next this postFinish ? prev prev prev prev 1 leftLen 1 rightLen 1 October 2020 OSU CSE 22

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend