Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 12

Homework 12: The Ordered List ADT
and Inheritance

Overview

In this homework, we build upon the array-based implementation of the List ADT that began in Homework 5. We use inheritance to specialize from our general notion of a list to include additional properties -- in this case the property in which each list element has a unique key and the sequential order of the list is based upon the ordering defined by the key value.

Attributes/Structure

The common properties of the collection comprising any list include:

  • It is homogenious -- the elements in the collection are all of the same type.
  • It has a finite length (the number of elements).
  • The elements are arranged sequentially:
    • There is a first element and a last element
    • Every element except the last has a unique successor
    • Every element except the first has a unique predecessor
  • Each list element has a unique key
  • For each element in the list, its predessor has a smaller key value and its successor has a larger key value.
  • In addition, each list has the notion of a positional indicator. This is used as a kind of cursor to indicate some current item in the list. The position can be any of the following: {first item, second item, ..., last item, EOL}, where EOL indicates the "End Of List", and is logically a position beyond the last item in the list. The position must always be one of the above set. For an empty list, the position must be EOL.
To these, we add the additional property that a list has a fixed maximum number of elements, which we refer to as the capacity of the list.

Operations

Note in the following description of the operations, I will use DT to abstractly represent the data type of the elements of the list. In the template impementation of this Ordered List class, this will be the passed template type.

Constructor/Destructor

OrdList ( )

Preconditions: None.
Postconditions: Creates an empty ordered list capable of holding DT type data items. Current position is at EOL. The capacity of the ordered list is a default constant MAX_ITEMS.

OrdList ( int max )

Preconditions: None.
Postconditions: Creates an empty ordered list capable of holding DT type data items. Current position is at EOL. The capacity of the ordered list is given by max .

OrdList ( const OrdList & orig )

Preconditions: None.
Postconditions: Creates a new ordered list object that is a copy of the given orig list. Note that the new ordered list shares no storage with the original list.

~OrdList ( )

Preconditions: None.
Postconditions: Destroys the ordered list, cleaning up all resources associated with the object.

Mutator(s)

void insert ( const DT & item )

Preconditions: Ordered List is not full (i.e. the number of items currently in the ordered list is less than its capacity).
Postconditions: The item has been inserted by copying into the list. Location of the insert occurs at the appropriate postion within the ordered list, based on key value. After an insert, the current position marks the newly inserted item. If a data item with the same key already exists in the list, it is replaced with the current item.

void remove ( )

Preconditions: Current position is not EOL.
Postconditions: The item at the current position is removed from the list. The current position becomes that of the item's successor, or EOL if the removed item were the last in the list.

bool search ( DT & searchItem )

Preconditions: None.
Postconditions: Searches the ordered list for a data item with key equal to that of searchItem. If the data item is found, then the cursor is moved to the data item, it is copied to searchItem, and the function returns true. Otherwise, the function returns false and the cursor remains in its current position and searchItem is not touched.

void clear ( )

Preconditions: None.
Postconditions: The ordered list is empty.

OrdList & operator= ( const OrdList & rhs )

Preconditions: None.
Postconditions: Previous ordered list object resources have been returned to the system and the ordered list has become a copy of the given OrdList rhs. The current ordered list is returned.

void reset ( )

Preconditions: None.
Postconditions: Resets the current position to the first item in the ordered list. If the ordered list is empty, then the position is set to EOL.

bool advance ( )

Preconditions: None.
Postconditions: Advances the current position to the next item in the ordered list. Advancing from EOL leaves the current position at EOL. If the position following the advance is not EOL, the result of the function is TRUE, and if the position following the advance is EOL, the result of the function is FALSE.

Observers (Predicates and Accessor(s))

bool isEmpty ( ) const

Preconditions: None.
Postconditions: Return value is true if the ordered list contains no items, and false otherwise.

bool isFull ( ) const

Preconditions: None.
Postconditions: Return value is true if the ordered list contains its capacity of items, and false otherwise.

Item getCurrent ( ) const

Preconditions: Current position is not EOL.
Postconditions: Return value is a copy of the item at the current position. The list is unaffected.

Assignment

Be sure and use the C++ Programming Style Guide and follow the conventions described there. Up to 35% of the grade for this homework will be based on following these conventions and practicing good documentation.

Your task is to implement the Ordered List ADT and to test it. The Ordered List ADT will inherit from the array based implementation of the List ADT. Many of the OrdList functions will inherit directly from List. These include remove(), clear(), operator=(), reset(), advance(), isEmpty(), isFull(), getCurrent(), and operator<<(). One new public function is provided in OrdList, search(), and one function has a different definition on OrdList from that in List, insert(). You should use public inheritance and may decide for yourself whether to make the data members of the List class protected, or to keep them private and use accessor/mutators for OrdList functions to use.

Since you need to search for items in both the search function and for inserts (to find the correct place), I recommend you implement a private binarySearch function that can be invoked from both places.

To provide flexibility, you should implement OrdList (and List) as template classes. To be able to perform comparisons in your search, the underlying DT type must support < and == relational operators. In your testing, you should include a non-primitive type to use for DT that demonstrates this as well as demonstrating on an ordered list of integers and an ordered list of strings.

The driver program should be interactive and general, and should allow operations including

  1. Creation of an ordered list object to be used by the driver program
  2. Insertion of a user supplied item into the list
  3. Removal of the item at the current position
  4. Display the item at the current position
  5. Go to the beginning of the list
  6. Advance to the next position in the list
  7. Query if the list is empty
  8. Query if the list is full
  9. Display the entire list
  10. Empty the entire list
  11. Demonstrate assignment overload
  12. Demonstrate copy constructor.

You will also detail a test plan that gives good coverage demonstrating that your implementation works and _then_ implement the test plan with a driver program named testOrdList.cpp.