Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each.

Similar presentations


Presentation on theme: "Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each."— Presentation transcript:

1 Chapter 3 ADTs unsorted List and Sorted List

2 3-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 3-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 3-4 Assumptions for All our Lists 1.Our lists are composed of unique elements. 2.When sorted, our lists are sorted from the smallest to largest key value. 3.We use the by copy approach. 4.We use the programming by contract approach.

5 Development of an Unsorted List ADT: UnsortedStringList

6 3-6 Unsorted List ADT Specification

7 3-7 Constructors

8 3-8 Observers

9 3-9 Transformers

10 3-10 Iterators

11 3-11 Application Level

12 3-12 List Design Terminology

13 3-13 Instance Variables of Unsorted List ADT

14 3-14 Beginning of Unsorted StringList Class:

15 3-15 Constructors

16 3-16 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.

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

18 3-18 isThere Operation

19 3-19 Retrieving an Item in an Unsorted List

20 3-20 Retrieving an Item in an Unsorted List

21 3-21

22 3-22 insert Operation

23 3-23 Deleting Bobby (move up) public void delete (String item) // Deletes the element that matches item from this list { int location = 0; while (item.compareTo(list[location]) != 0) location++; If(isThere(item) { For( int I = location; i,

24 3-24 Deleting Bobby (swap)more efficient public void delete (String item) // Deletes the element that matches item from this list { int location = 0; while (item.compareTo(list[location]) != 0) location++; list[location] = list[numItems - 1]; numItems--; }

25 3-25

26 3-26 UML Diagram of UnsortedStringList

27 3-27 Reuse Operations Ways we could reuse the code of the Unsorted List ADT to create the code for the Sorted List ADT: 1.Cut and Pastecut and paste the code that we are able to reuse into the new file. 2.Direct Inheritancehave the Sorted List ADT inherit methods from the Unsorted List ADT. 3.Abstract Classesresolve the deficiencies of both of the previous approaches.

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

29 3-29 The Abstract Class Please click on the following link Programs/C03P165.jpg to view the appropriate program. Programs/C03P165.jpg

30 3-30 Extending the Abstract Class Please click on the following link Programs/C03P166.jpg to view the appropriate program. Programs/C03P166.jpg

31 3-31 UML Diagram

32 Abstract Data Type Sorted List

33 3-33 Sorted List ADT Specification (partial)

34 3-34 Constructors

35 3-35 Redefined insert

36 3-36 Redefined Delete

37 3-37 insert Operation 1.Find the place where the new element begins. 2.Create space for the new element. 3.Put the new element on the list.

38 3-38 Original List

39 3-39 Insert Becca

40 3-40 Result

41 3-41 insert (item)

42 3-42

43 3-43 delete (item)

44 3-44

45 3-45 Binary Search Algorithm

46 3-46 isThere (item): returns boolean

47 3-47

48 3-48

49 3-49

50 3-50

51 3-51

52 3-52 UML Diagram Please click on the following link Programs/C03P180.jpg to view the appropriate program. Programs/C03P180.jpg

53 3-53 Comparison of Algorithms Big-O Notation A notation that expresses computing time (complexity) as the term in a function that increases most rapidly relative to the size of a problem If f(N) = N N N + 50 then f(N) is 0(N 4 ). N represents the size of the problem.

54 3-54 Comparison of Rates of Growth

55 3-55 Comparison of Linear and Binary Searches

56 3-56 Big-O Comparison of List Operations

57 3-57 Generic ADTs So far… An unsorted list of strings An unsorted list of strings that extended String List A sorted list of strings that extended String List Next Lists of generic data Generic Data Type A type for which the operations are defined but the types of the items being manipulated are not

58 3-58 The Listable Interface

59 3-59 A ListCircle Class

60 3-60 A Generic Abstract List Class Please click on the following link Programs/C03P196.jpg to view the appropriate program. Programs/C03P196.jpg

61 3-61

62 3-62 A Generic Sorted List ADT Please click on the following link Programs/C03P200.jpg to view the appropriate program. Programs/C03P200.jpg

63 3-63 UML Diagrams for our List Framework

64 3-64 A Listable Class Please click on the following link Programs/C03P204.jpg to view the appropriate program. Programs/C03P204.jpg

65 3-65 Using the Generic List To Create a sorted list of strings use either of its constructors: SortedList list1 = new SortedList(); SortedList list2 = new SortedList (size); Declare at least one object of class ListString ListString aString; Instantiate ListString objects and place them on the list. aString = new ListString(Amy); list.insert(astring)


Download ppt "Chapter 3 ADTs unsorted List and Sorted List. 3-2 List Definitions Linear relationship Each element except the first has a unique predecessor, and each."

Similar presentations


Ads by Google