110 likes | 235 Views
Lab 8. Ordered list. OVERVIEW. In an ordered list the elements are maintained in ascending (or descending) order based on the data contained in the list elements. Typically, the contents of one field are used to determine
E N D
Lab 8 Ordered list
OVERVIEW In an ordered list the elements are maintained in ascending (or descending) order based on the data contained in the list elements. Typically, the contents of one field are used to determine the ordering. This field is referred to as the key field, or the key. In this laboratory, we assume that each element in an ordered list has a key that uniquely identifies the element that is, no two elements in any ordered list have the same key. As a result, you can use an element’s key to efficiently retrieve the element from a list.
Elements The elements in an ordered list are of generic type ListData. Objects in the ordered list must support a method called key() that returns an element’s key.
Structure • The list elements are stored in ascending order based on their keys. • For each list element E, the element that precedes E has a key that is less than E’s key and the element that follows E has a key that is greater than E’s key.
Constructors OrdList ( ) Precondition: None. Postcondition: Default Constructor. Calls the default constructor of its superclass, which creates an empty list. Allocates enough memory for a list containing DEF_MAX_LIST_SIZE (a constant value) elements. OrdList ( intmaxNumber ) Precondition: None. Postcondition: Constructor. Calls the corresponding superclass constructor, which creates an empty list. Allocates enough memory for a list containing maxNumber elements.
Methods void insert ( ListDatanewElement ) Precondition: List is not full. Postcondition: Inserts newElement in its appropriate position within a list. If an element with the same key as newElement already exists in the list, then updates that element’s nonkey fields with newElement’snonkeyÞelds. Moves the cursor to newElement. ListData retrieve ( intsearchKey ) Precondition: None. Postcondition: Searches a list for the element with key searchKey. If the element is found, then moves the cursor to the element and returns its value. Otherwise, does not move the cursor and returns null to indicate that searchElement is undefined.
Methods void remove ( ) Precondition: List is not empty. Postcondition: Removes the element marked by the cursor from a list. If the resulting list is not empty,then moves the cursor to the element that followed the deleted element. If the deleted elementwas at the end of the list, then moves the cursor to the beginning of the list. void replace ( ListDatanewElement ) Precondition: List is not empty. Postcondition: Replaces the element marked by the cursor with newElement. Note that this entails removing the element and inserting newElement in its correct ordered-list position. Moves the cursor to newElement. void clear ( ) Precondition: None. Postcondition: Removes all the elements in a list. booleanisEmpty ( ) Precondition: None. Postcondition: Returns true if a list is empty. Otherwise, returns false.
Methods booleanisFull ( ) Precondition: None. Postcondition: Returns true if a list is full. Otherwise, returns false. booleangotoBeginning ( ) Precondition: None. Postcondition: If a list is not empty, then moves the cursor to the element at the beginning of the list and returns true. Otherwise, returns false. booleangotoEnd ( ) Precondition: None. Postcondition: If a list is not empty, then moves the cursor to the element at the end of the list and returns true. Otherwise, returns false. .
Methods booleangotoNext ( ) Precondition: List is not empty. Postcondition: If the cursor is not at the end of a list, then moves the cursor to the next element in the list and returns true. Otherwise, returns false booleangotoPrior ( ) Precondition: List is not empty. Postcondition: If the cursor is not at the beginning of a list, then moves the cursor to the preceding element in the list and returns true. Otherwise, returns false.
Methods Object getCursor ( ) Precondition: List is not empty. Postcondition: Returns a copy of the element marked by the cursor. void showStructure ( ) Precondition: None. Postcondition: Outputs the keys of the elements in a list. If the list is empty, outputs Empty list. Note that this operation is intended for testing/debugging purposes only. It only supports keys that are one of JavaÕs primitive data types (int, char, and so forth).
You should .. Step 1: Implement the operations in the Ordered List ADT and the revised array-based List ADT. Base your implementation on the incomplete class definitions from the files OrdList.jshl and ListArray.jshl. The interface for the List class is provided in the file List.java. Note that you only need to create implementations of the constructors, insert, replace, and retrieve operations for the Ordered List ADT; the remainder of the operations are inherited from your array implementation of the ListArray ADT. Your implementations of the insert and retrieve operations should use the binarySearch() method to locate an element. An implementation of the binary search algorithm and the showStructure operation is given in the file OrdList.jshl. If you did not complete Laboratory 4 earlier, then implement each method in the ListArray class according to the method comments given in ListArray.jshl along with the descriptions given in this laboratory for the Ordered List ADT methods that are not overridden by the OrdListclass. Descriptions for all the OrdList class methods (inherited, overridden, and those unique to OrdList) are given at the beginning of this laboratory. Step 2: Save your implementation of the Ordered List ADT and the array-based List ADT in the files OrdList.java and ListArray.java, respectively. Be sure to document your code.