Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 5

Homework 5: The List ADT

Overview

The list is one of the most frequently used data structures. Although all programs share the same definition of list -- a sequence of like data items -- the type of data item stored in lists varies from program to program. Some use lists of integers, others use lists of characters, floating point numbers, or more complex base type objects. Initially, we will decide at ADT class implementation time the type of the data items for a list. Later, in a reimplementation of the List ADT, we will use C++ templates so that the user of a List can specify the item data type.

The common properties of the collection comprising any such 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
To these, we add the additional property that a list has a fixed maximum number of elements, which we denote symbolically with MAX_ITEMS.

Attributes

Structure

The key structure associated with the List ADT is the sequential notion of the collection referred to above -- with each data item except the first having a unique predecessor and each data item except the last having a unique successor.

Operations

Subordinate Item Type

Relation: an enumerated type with LESS, GREATER, and EQUAL

Item: a class encapsulating the type of the data items. This class must provide a method named comparedTo, which returns a Relation and takes another Item and is able to compare the current item object with the passed item object, returning LESS if current < passed, GREATER if current > passed, and EQUAL if current is equal to passed.

Dependent Definitions

MAX_ITEMS: a named constant specifying the maximum number of items in a list.

Constructor/Destructor

List ( )

Preconditions: None.
Postconditions: Creates an empty list capable of holding Item type data items. Current position is at EOL.

~List ( )

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

Mutator(s)

void insert ( const Item & item )

Preconditions: List is not full (i.e. the number of items currently in the list is less than MAX_ITEMS).
Postconditions: The item has been inserted by copying into the list. Location of the insert occurs before the current position. After an insert, the current position follows the inserted 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.

void reset ( )

Preconditions: None.
Postconditions: Resets the current position to the first item in the list. If the 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 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 list contains no items, and false otherwise.

bool isFull ( ) const

Preconditions: None.
Postconditions: Return value is true if the list contains MAX_ITEMS 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 List ADT, and in order to do so and to test, to implement the Item class and a driver program. To do this you will create a header file, named listarray.h which contains the class definition, and an implementation file, named listarray.cpp, which contains all of the definitions of the member functions corresponding to the operations described above. Likewise, you will create files item.h and item.cpp to wrap the basic type of items within a class. Start with integers as the attribute of an Item.

Use an array implementation for the underlying data structure of the List. You should use dynamic allocation at object creation time, and should return to free space the array at object destruction.

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

  1. Creation of a 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

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