1 / 11

Lab 8

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

fifi
Télécharger la présentation

Lab 8

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. 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 element’s 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 element’s 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 E’s key and the element that follows E has a key that is greater than E’s 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 ( intmaxNumber ) 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 ( 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.

  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 ( 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.

  8. 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. .

  9. 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.

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

More Related