1. //driver.cpp
//written to test doubly-linked list ADT
//ItemType must have << and >> operators defined
#include <fstream>
#include <iostream>
#include <string>
#include "sortlist.h"
using namespace std;
template <class ItemType>
void PrintAscending(ostream& dataFile,
SortedType<ItemType>& list, string listName);
//pre: list has been initialized
// dataFile is open for output
//post: each component in list has been written to
dataFile
// dataFile is still open
template <class ItemType>
void PrintDescending(ostream& dataFile,
SortedType<ItemType>& list, string listName);
//pre: list has been initialized
// dataFile is open for output
//post: each component in list has been written, in
reverse order, to dataFile
// dataFile is still open
void DisplayMenu(int& choice);
//pre: listA and listB have been initialized
//post: displays a menu for list operations
2. template <class ItemType>
void Insert(SortedType<ItemType>& list, string
listName);
//pre: list has been initialized
// item to insert is entered by the user in the
function
//post: Calls the ADT InsertItem and prints a message
indicating
// success or failure
template <class ItemType>
void Delete(SortedType<ItemType>& list, string
listName);
//pre: list has been initialized
// item to delete is entered by the user within
the function
//post: Calls the ADT delete function and prints a
message
// indicating success or failure.
template <class ItemType>
void Retrieve(SortedType<ItemType> list);
//pre: none
//post: prompts user for item to retrieve from list;
displays message
// indicating whether or not item was found in
the list
int main()
{
3. SortedType<int> listA, listB, listC;
int choice;
while (true)
{
DisplayMenu(choice);
switch (choice)
{
case 0: return 0;
case 1: Insert(listA, "A");
break;
case 2: Insert(listB, "B");
break;
case 3: Delete(listA, "A");
break;
case 4: listA.MakeEmpty();
break;
case 5: Retrieve(listA);
break;
case 6: listC = listB = listA;
break;
case 7: listA.RemoveFirst();
break;
case 8: listA.RemoveLast();
break;
4. case 9: PrintAscending(cout, listA,
"A");
PrintAscending(cout, listB,
"B");
PrintAscending(cout, listC,
"C");
break;
case 10: PrintDescending(cout, listA,
"A");
PrintDescending(cout, listB,
"B");
PrintDescending(cout, listC,
"C");
break;
default: return 0;
}//end switch
}//end while
}//end main
template <class ItemType>
void PrintAscending(ostream& dataFile,
SortedType<ItemType>& list, string listName)
{
ItemType item;
int length = list.LengthIs();
if (length == 0)
{
6. for (int count=0; count<length; count++)
{
list.GetPreviousItem(item);
dataFile << item;
dataFile << " ";
}
dataFile << endl;
}
void DisplayMenu(int& choice)
{
cout << "Please select from the menu.nn";
cout << "1tInsert an item to List A.n";
cout << "2tInsert an item to List B.n";
cout << "3tDelete an item from List A.n";
cout << "4tDelete all items from List A. (Make
listA empty)n";
cout << "5tRetrieve an item from List A.n";
cout << "6tPerform assignment: listC = listB =
listAn";
cout << "7tDelete first element of List A.n";
cout << "8tDelete last element of List A.n";
cout << "9tPrint lists in ascending order.n";
cout << "10tPrint lists in descending order.n";
cout << "0tExit programnn";
cin >> choice;
}
template <class ItemType>
7. void Insert(SortedType<ItemType>& list, string
listName)
{
ItemType item;
cout << "Please enter the item to insert: ";
cin >> item;
if (list.InsertItem(item))
cout << item << " has been inserted into list
" << listName << ".n";
else
cout << "Unable to add " << item << " to " <<
listName << ".n";
}
template <class ItemType>
void Delete(SortedType<ItemType>& list, string
listName)
{
ItemType item;
cout << "Please enter the item to delete: ";
cin >> item;
if (list.DeleteItem(item))
cout << item << " has been deleted from list
" << listName << ".n";
else
cout << item << " was not in the list. List
has not been changed.n";
}
template <class ItemType>
void Retrieve(SortedType<ItemType> list)
{
8. ItemType item;
bool found;
cout << "Please enter the item to be
retrieved:n";
cin >> item;
list.RetrieveItem(item, found);
if (found)
cout << item << " was in the list.n";
else
cout << item << " was not in the list.n";
}
9. // Implementation file for Sorted List ADT.
// Class specification is in file sortlist.h.
// Class is templated.
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
NodeType* back;
};
template <class ItemType>
SortedType<ItemType>::SortedType ()
{
length = 0;
listData = NULL;
listRear = NULL;
}
template <class ItemType>
SortedType<ItemType>::~SortedType ()
{
NodeType<ItemType>* tempPtr;
while(listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
14. found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
if (location->info < item)
{
location = location->next;
moreToSearch = (location != NULL);
}
else if (item == location->info)
{
found = true;
item = location->info;
}
else
moreToSearch = false;
}
}
template <class ItemType>
bool SortedType<ItemType>::InsertItem(ItemType item)
{
//modify the following code so that the function
returns false
//if item is already in the list
//Prepare node for insertion
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
if (newNode == NULL)
return false;
newNode->info = item;
15. //check if inserting into empty list
if (listData == NULL)
{
newNode->next = NULL;
newNode->back = NULL;
listData = newNode;
listRear = newNode;
}
else if(item ==listData->info)
return false;
//check if inserting before existing first item
else if (item < listData->info)
{
newNode->next = listData;
newNode->back = NULL;
listData->back = newNode;
listData = newNode;
}
//check if inserting after last item
else if (item > listRear->info)
{
newNode->next = NULL;
newNode->back = listRear;
listRear->next = newNode;
listRear = newNode;
}
else//inserting between existing nodes
{
// Find node that should precede newNode
NodeType<ItemType>* location;
location = listData;
while (location->next->info < item)
location = location->next;
21. // sortlist.h: Header file for Sorted List ADT.
// Class is templated.
// Assumption: ItemType is a type for which the
comparison operators
// are defined
#ifndef SORTLIST_H
#define SORTLIST_H
template <class ItemType>
struct NodeType;
template <class ItemType>
class SortedType
{
public:
SortedType (); // Class constructor
~SortedType (); // Class destructor
SortedType(const SortedType& source);
//copy constructor
SortedType operator =(const SortedType& rhs);
//assignment operator
bool IsFull() const;
// Function: Determines whether list is full.
// Post: Function value = (list is full)
22. int LengthIs() const;
// Function: Determines the number of elements in
list.
// Post: Function value = number of elements in
list.
void MakeEmpty();
// Function: Initializes list to empty state.
// Post: List is empty.
void RetrieveItem(ItemType& item, bool& found);
// Function: Retrieves list element whose key
matches item's key (if present).
// Pre: Key member of item is initialized.
// Post: If there is an element someItem whose
key matches item's key,
// then found = true and item is a copy of
someItem; otherwise
// found = false and item is unchanged.
// List is unchanged.
bool InsertItem(ItemType item);
// Function: Adds item to list
// Pre: List has been initialized
// Post: Returns true if the item was
successfully added
// returns false if not able to add
void RemoveFirst();
//pre: none
//post: if list is not empty, first element has
been removed,
// else list is unchanged
23. void RemoveLast();
//pre: none
//post: if list is not empty, last element has
been removed,
// else list is unchanged
bool DeleteItem(ItemType item);
// Function: Deletes the element whose key
matches item's key.
// Pre: Key member of item is initialized.
// At most one element in list has a key
matching item's key.
// Post: No element in list has a key matching
item's key.
void ResetList();
// Function: Initializes current position for an
iteration through the list.
// Pre: List has been initialized
// Post: Current position is prior to first
element of list.
void GetNextItem(ItemType&);
// Function: Gets the next element in list.
// Pre: Current position is defined.
// Element at current position is not last
in list.
// Post: Current position is updated to next
position.
// item is a copy of element at current
position.
void ResetListBackward();
24. // Function: Initializes current position for a
backward iteration through the list.
// Pre: List has been initialized
// Post: Current position is after the last
element of the list
void GetPreviousItem(ItemType&);
// Function: Gets the previous element in list.
// Pre: Current position is defined.
// Element at current position is not first
in list.
// Post: Current position is updated to previous
position.
// item is a copy of element at current
position.
private:
int length;
NodeType<ItemType>* listData;
NodeType<ItemType>* listRear;
NodeType<ItemType>* currentPos; //for forward
traversal
NodeType<ItemType>* currentRev; //for backward
traversal
};
#include "sortlist.cpp"
#endif