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:
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. The capacity of any list is always no more than MAX_ITEMS.
- 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
Attributes
- As described above, the primary data item/attributes of a List object is a finite length homogeneous collection of data items arranged in sequential fashion.
- 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.
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
Item: We will use a type name of Item to denote the type of the elelments within a List. We can use a "type synonym" facility of C++ called typedef which will allow us to make the Item type a synonym for an existing type. For more complex types, we can define Item to be a struct which gathers together the data representing each instance of the type. Finally, if we want to include operations associated with the Item type, we can define Item to be a class encapsulating the type of the data items. For the first two styles of Item type definition, we can include these type declarations in the class definition file for the List class (i.e. in List.h). For the third, we would create a separate class definition file and implementation file for the Item class type.
Typedef example: typedef int Item; or typedef char Item;
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. The List has a capacity of at most MAX_ITEMS items.List ( int max )
Preconditions: 0 <= max <= MAX_ITEMS.
Postconditions: Creates an empty list capable of holding Item type data items. Current position is at EOL. The List has a capacity of at most max items.~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 its capacity).
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 atEOL ( ) const
Preconditions: None.
Postconditions: Return value is true if the current position is logically at EOL, and false otherwise.bool isFull ( ) const
Preconditions: None.
Postconditions: Return value is true if the 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 25% 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, 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. Start with integers as the (typedef) base type 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 allow a sequence of single character commands by which the user instructs the driver program which operations to exercise. The operations should include the following:
Test List Driver Commands Command Action CcCreate a new List object (using the new C++ operator), making it the current List; the command is immediately followed by an integer (c) which defines the capacity of the list. For a list with 0 specified capacity, create a "default" (MAX_SIZE) capacitly list. DDestroy the current List object +xThe '+' command performs an insert operation on the current list. The value to insert (x) immediately follows the '+' and is of type consistent with the type of the list (an integer list for your submission). In general, this should be read into an Item typed variable. -Perform a remove operation at the current cursor position . @Display to cout the data item at the current cursor position (or the character sequence "EOL" if at the end of the list). *Display the entire list, including an indication of the current cursor position. <Go to the beginning of the list. NAdvance the cursor to the next item in the list. EReport whether the list is empty. FReport whether the list is full. $Report whether the current position is EOL. XDelete all elements in the list. HPrint a help message with the supported commands and a brief description of their actions. QQuit the test program. All commands will be separated by whitespace but may be combined on a single line. Other than an initial prompt, do not include prompts after processing each command.
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.
