Homework 4: 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/Domain
- 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.
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.Print/String
void display ( ostream & stream ) const
Preconditions: Item type supports stream insertion to an output stream.
Postconditions: Outputs the contents of the list to the given stream. Form of the output must be strictly adhered to. The output uses '(' and ')' to denote the beginning and the end of the list. The output use space separation between elements of the list as well as between the beginning '(' and the first element, and the ending ')' and the last element. If the current position is not EOL, the element should be displayed between vertical bars ('|'). If the current position is EOL, this should be denoted by two vertical bars together at the end of the list. The list display should terminate with a newline. Note that this operation should not change the internal data associated with the list, and that includes the cursor position. Examples:
- Empty List -- ( || )
- One element list with cursor at EOL -- ( a || )
- One element list with cursor at that element -- ( |a| )
- Two element list with cursor at second element -- ( a |b| )
- Two element list with cursor at end -- ( a b || )
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 MUST 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 to cout the entire list, including an indication of the current cursor position. Output should conform to the specification above. <Go to the beginning of the list. NAdvance the cursor to the next item in the list. EReport whether the list is empty, printing TRUE or FALSE. FReport whether the list is full, printing TRUE or FALSE. $Report whether the current position is EOL, printing TRUE or FALSE. 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. With each command read, output the command in a line that reads 'Command: <command-with-arg>, followed by a newline and then by any output required of the command.
While debugging, your individual commands may include additional output to help you check whether things are working correctly. However, your final submission should only print output for the commands that specify as such: '@', '*', 'E', 'F', and '$'.
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.
Helpful Files
File Description/Use ListArray.h Header/Interface file conforming to the above description. You may use this header file or write one of your own. Any difference would be in the choices for the private section of the class declaration. hw4Test.o This is the object file for my test program. If your List class conforms to the public interface given in the above ListArray.h file and you create your implementation file for the List class in a file named ListArray.cpp, then you could test your List class with my client driver, even before you have written your own driver. To create an executable for the above scenario, you would issue the following command:
g++ -o testList ListArray.cpp hw4Test.o
which compiles your ListArray.cpp file and combines it with my hw4Test.o object file into an executable named testList.
hw4Test This is a complete executable for a List of ints as required by this assignment. It does not implement the H (help) command, but all other driver actions are available. You can use this if you have any questions about how the driver should work.
When you download an executable like this from the Web, Linux needs to be "informed" that his is an executable file. This can be accomplished with the following command:
chmod u+x hw4Test
and you should do this after downloading and before trying to execute this test program.