1 / 78

Chapter 3

Chapter 3. ADTs unsorted List and Sorted List. List Definitions. Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. Length The number of items in a list; the length can vary over time. List Definitions.

jenaya
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 ADTs unsorted List and Sorted List

  2. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. Length The number of items in a list; the length can vary over time.

  3. List Definitions Unsorted list A list in which data items are placed in no particular order; the only relationship between data elements is the list predecessor and successor relationships. Sorted list A list that is sorted by the value in the key; there is a semantic relationship among the keys of the items in the list. Key The attributes that are used to determine the logical order of the list.

  4. Assumptions for All our Lists • Our lists are composed of unique elements. • When sorted, our lists are sorted from the smallest to largest key value. • We use the “by copy” approach.

  5. Development of an Unsorted List ADT: UnsortedStringList

  6. Unsorted List ADT Specification

  7. Constructors

  8. Observers

  9. Transformers

  10. Iterators

  11. Application Level

  12. List Design Terminology

  13. Constructors

  14. Definitions • Signature The distinguishing features of a method heading. The combination of a method name with the number and type(s) of its parameters in their given order. • Overloading The repeated use of a method name with a different signature.

  15. Simple Observers public boolean isFull ( ) // Returns whether this lis is full { return (list.length == numItems); }

  16. isThere Operation

  17. Retrieving an Item in an Unsorted List

  18. Retrieving an Item in an Unsorted List

  19. insert Operation

  20. Deleting Bobby (move up) • Requires moving all of the names after Bobby. • In the worst case, the whole list could be moved • The last name becomes part of the garbage.

  21. Deleting Bobby (swap)—more efficient • The idea is to swap the last element with the one you want to delete. • Then decrease the size of the list. • This only works for unsorted lists. • A sorted list has many advantages over an unsorted list, but deleting in unsorted lists is more efficient. Its one advantage.

  22. UML Diagram of UnsortedStringList

  23. Reuse Operations Ways we could reuse the code of the Unsorted List ADT to create the code for the Sorted List ADT: • Cut and Paste—”cut and paste” the code that we are able to reuse into the new file. • Direct Inheritance—have the Sorted List ADT inherit methods from the Unsorted List ADT. • Abstract Classes—resolve the deficiencies of both of the previousapproaches.

  24. Steps for Using Abstract Class Approach • We first create an abstract list class. • Its concrete methods provide the operations that our two list ADTs share in common. • Its abstract methods provide the operations that are not shared. • Then create two concrete classes that extend the abstract list class. • One that implements an unsorted list • The other that implements a sorted list

  25. UML Diagram

  26. Abstract Data Type Sorted List

  27. Sorted List ADT Specification (partial)

  28. Constructors

  29. Redefined insert

  30. Redefined Delete

  31. insert Operation • Find the place where the new element begins. • Create space for the new element. • Put the new element on the list.

  32. Original List

  33. Insert Becca

  34. Result

  35. insert (item)

  36. delete (item)

More Related