Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 10

Homework 10: The List ADT Revisited
Doubly Linked Implementation and Recursive Functions

Overview

In this homework, you will revise your List ADT implementation and enhance it by revising your linked list implementation as a doubly linked implementation and by defining it as a template class. In addition, we will use the List implementation as the basis for writing recursive functions. Some of these will be realizations of the functions we developed in class, and then we will add a few more.

Attributes/Structure

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 the List class, this will be the passed template type.

Constructor/Destructor

List ( )

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

List ( const List & orig )

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

~List ( )

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

Mutator(s)

void insert ( const DT & item )

Preconditions: None.
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 insertEnd ( const DT & item )

Preconditions: None.
Postconditions: The item has been inserted by copying into the list. Location of the insert occurs at the end of the list. Current position is at EOL following the insert.

void insertBefore ( const DT & item, const DT & successor)

Preconditions: Datatype DT must allow '==' equality checking between items.
Postconditions: The item has been inserted by copying into the list immediately preceeding the first occurence of the given successor item. If successor is not found, the list is unaffected. Current position after a successful insert is at the successor 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 was the last in the list.

void clear ( )

Preconditions: None.
Postconditions: The list is empty and all Node resources have been returned to the system.

void reverse ( )

Preconditions: None.
Postconditions: The list has been reversed in order (so a list with a b c would be rewritten as c b a). This should be accomplished by using the existing items in the list. (I.e. do not create new Node items.)

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.

List& operator= ( const List & rhs )

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

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: Returns false.

bool atEOL ( ) const

Preconditions: None.
Postconditions: Return value is true if the current position is logically at EOL, and false otherwise.

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

int length ( ) const

Preconditions: None.
Postconditions: Returns the length of the list. The list is unaffected.

void print ( ostream & os ) const

Preconditions: None.
Postconditions: The list has been output to the given ostream. The list is unaffected, so the current cursor postion is exactly what it was prior to the operation.

void printMirror ( ostream & os ) const

Preconditions: Objects of type DT can be output using stream insertion to the given ostream.
Postconditions: The data items in the list are output to the given ostream. The function outputs the items from beginning to end and then from end back to beginning. The list is unaffected.

Assignment

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

Your task is to (re)implement the List ADT using a double linked list implementation. You must use a template class definition so that we can easily define different types of list in the same program. You are also required to implement a non-member function that overloads the stream insertion operator for List objects. Please be sure that the stream insertion indicates element in the sequence is the beginning, end, and current position of the list.

The following functions should be designed using recursion: length(), print(), printMirror(), insertEnd(), insertBefore(), and reverse().

As before, the driver program should be general, and should allow the following operations:

Test List Driver Commands
Command
Action
C
Create a new List object (using the new C++ operator), making it the current List.
D
Destroy the current List object.
+x
The '+' 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 (a char list for your submission). In general, this should be read into an DT typed variable.
>x
Insert the value x at the end of the list.
&x,y
Insert the value x as the immediate predecessor of y, assuming y is found in the list.
-
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. This (either directly or indirectly should invoke the print function. Include an indication of the current cursor position.
^
Print the mirror of the list. This need not indicate the current cursor position.
<
Go to the beginning of the list.
N
Advance the cursor to the next item in the list.
E
Report whether the list is empty.
F
Report whether the list is full.
$
Report whether the current position is EOL.
T
Toggle the current list with an alternative list.
=
Demonstrate assignment overload. The active stack should be the left hand side of the assignment and the alternative stack should be the right hand side.
#
Demonstrate copy constructor. This creates a new List object as the current List and the alternate list is used as the source (or original) from which the new List gets its contents.
X
Delete all elements in the list.
H
Print a help message with the supported commands and a brief description of their actions.
Q
Quit the test program.

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.

Please instantiate your template List at least as a List of chars.