650 likes | 793 Views
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.
E N D
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 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.
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. • We use the programming “by contract” approach.
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.
Simple Observers public boolean isFull ( ) // Returns whether this lis is full { return (list.length == numItems); }
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,<numItm-1;i++) • list[i] = list[i+1]; • } • }
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--; • }
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.
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
The Abstract Class • Please click on the following link Programs/C03P165.jpg to view the appropriate program.
Extending the Abstract Class • Please click on the following link Programs/C03P166.jpg to view the appropriate program.
insert Operation • Find the place where the new element begins. • Create space for the new element. • Put the new element on the list.