Homework 8: The List and SortedList ADTs
Doubly Linked List and Template Implementations
Overview
In Homework 5, you created an implementation of the List ADT using a singly linked list to store the list data items. By including the predcursor as part of the List data members, we were able to implement both insert() and remove() efficiently. However, we did not include operations for going to the back of the list and for traversing in a backward direction, because our implementation did not support such operations easily or efficiently.
In this homework, we will use a doubly linked list to address these deficiencies. We will also implement a SortedList abstraction that is built using this expanded List ADT. Finally, we will create template versions of both so that we can add versatility as well as see how one template class can be built from an underlying template class.
The homework divides itself cleanly into three phases, each of which are relatively straightforward modifications and enhancements to earlier work or to a prior step. You are well advised, however, to implement and test each of the phases incrementally, so that you do not face an overload of work just before the assignment is due.
Phase 1: Enhance your List ADT to a double-linked implementation and augment the interface with operations toEOL(), retreat(), and find(). This work should be completed by early in the weekend. To support this, we need to add driver commands '>' for toEOL(), 'P' for retreat(), and '?x' to find item x. Put the interface file in ListDLink.h (with class name List), and the implementation file in ListDLink.cpp.
Phase 2: Create a SortedList ADT. The SortedList ADT has the same operations as the List ADT, but the semantics of insert() change. The insert(item) must perform its function not at the current cursor position, but at the place in the list where all elements earlier in the list have a "value" (as determined by "<", "==" and "<=" on the base type) < item, and all elements that succeed item in the list are >= item. The interface file should be SortedList.h and the implementation file should be SortedList.cpp, and the class name is also SortedList. You should target to have Phase 2 completed by Monday afternoon.
Phase 3: Create a template version of the List ADT, and then use that template List ADT to create a template version of the SortedList ADT. Use the same filenames as employed for Phase 1 and 2 above for this. You will also create a List_Impl.cpp and a SortedList_Impl.cpp file that will explicitly instantiate int and char versions of both of these template classes.
Phase 1 List ADT (with update for SortedList insert() semantics)
List ( )
Preconditions: None.
Postconditions: Creates an empty list capable of holding DT type data items. Current position is at EOL. We no longer will worry about capacity.List ( const List & src )
Preconditions: None
Postconditions: Creates a new List which is a deep copy of the passed src List.~List ( )
Preconditions: None.
Postconditions: Destroys the list, cleaning up all resources associated with the object.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.For SortedList, the Postconditions of insert: The item has been inserted by copying into the list. Location of the insert occurs at the appropriate position within the SortedList, so that all preceeding elements are < item and all succeeding elements are >= item. After an insert, the current position marks the newly inserted item. Multiple items with the same value are permitted.
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 find ( DT & searchitem )
Preconditions: None.
Postconditions: Searches the 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, and the function returns true. Otherwise, the function returns false and the cursor remains in its current position.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.void toEOL ( )
Preconditions: None.
Postconditions: Resets the current position to the end of list (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.bool retreat ( )
Preconditions: None.
Postconditions: Retreats the current position to the prior item in the list. Retreatomg from the first item in the list leaves the current position at the first item. If the position following the retreat is not the first item in the list (or EOL for an empty list), the result of the function is TRUE, and if the position following the advance is the first item in the list, the result of the function is FALSE.List & operator= ( const List & rhs )
Preconditions: None.
bool isEmpty ( ) const
Postconditions: Previous list object resources have been returned to the system and the list has become a copy of the given rhs. The current list is returned.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 always false.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.void display ( ostream & stream ) const
Preconditions: DT 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 uses 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.Driver
As before, the driver program should be general. It should allow the notion of two List objects, the "current" list and the "alternate" list. Single List object operations pertain to the "current" list. For the copy constructor and the assignment overload, the "current" list is the one that gets updated (is logically the lhs), and the "alternate" list is the one that is copied from (is logically the rhs). There is also a command to switch the roles of the two lists, making the current the alternate and the alternate the current.
The submitted driver should be designed for the template version of the SortedList class, instantiated for char lists. You should also test the earlier phases, but these need not be submitted.
Command |
Action |
---|---|
C |
Create a new List object (using the new C++ operator), making it the current List. This always invokes the default constructor. |
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 (an integer list for your submission). In general, this should be read into an DT 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 given previously. |
< |
Go to the beginning of the list. |
> |
Go to EOL. |
?x |
Search for the given item (x). Report the value of the search function, TRUE if x was found or FALSE if x was not found. |
N |
Advance the cursor to the next item in the list. |
P |
Retreat the cursor to the previous item in the list. |
E |
Report whether the list is empty, printing TRUE or FALSE. |
F |
Report whether the list is full, printing TRUE or FALSE. |
$ |
Report whether the current position is EOL, printing TRUE or FALSE. |
= |
Demonstrate assignment overload, assigning the alternate list to the current list. |
& |
Demonstrate copy constructor, creating a new current list from the alternate list. |
~ |
Switch the current list with the alternate list. |
# |
Ignore all following characters up to the following newline, outputing these characters as its argument. |
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.