Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lab 8 Ordered list

2 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 elements key to efficiently retrieve the element from a list.

3 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 elements key.

4 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 Es key and the element that follows E has a key that is greater than Es key.

5 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 ( int maxNumber ) Precondition: None. Postcondition: Constructor. Calls the corresponding superclass constructor, which creates an empty list. Allocates enough memory for a list containing maxNumber elements.

6 Methods void insert ( ListData newElement ) 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 elements nonkey fields with newElements nonkey Þelds. Moves the cursor to newElement. ListData retrieve ( int searchKey ) 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.

7 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 ( ListData newElement ) 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. boolean isEmpty ( ) Precondition: None. Postcondition: Returns true if a list is empty. Otherwise, returns false.

8 Methods boolean isFull ( ) Precondition: None. Postcondition: Returns true if a list is full. Otherwise, returns false. boolean gotoBeginning ( ) 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. boolean gotoEnd ( ) 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..

9 Methods boolean gotoNext ( ) 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 boolean gotoPrior ( ) 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.

10 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).

11 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 OrdList class. 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.


Download ppt "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."

Similar presentations


Ads by Google