Download - C++ detyrat postim_slideshare
//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
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(){
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;
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){
dataFile << "List " << listName << " is empty.\n";
return;} dataFile << "List " << listName << "
contains the following items:\n";list.ResetList();for (int count=0; count<length; count++){
list.GetNextItem(item);dataFile << item;dataFile << " ";
}dataFile << endl;
}
template <class ItemType>void PrintDescending(ostream& dataFile, SortedType<ItemType>& list, string listName){
ItemType item;int length = list.LengthIs();
if (length == 0){
dataFile << "List " << listName << " is empty.\n";
return;} dataFile << "List " << listName << "
contains the following items:\n";list.ResetListBackward();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.\n\n";
cout << "1\tInsert an item to List A.\n";cout << "2\tInsert an item to List B.\n";cout << "3\tDelete an item from List A.\n";cout << "4\tDelete all items from List A.
(Make listA empty)\n";cout << "5\tRetrieve an item from List A.\
n";cout << "6\tPerform assignment: listC =
listB = listA\n";cout << "7\tDelete first element of List A.\
n";cout << "8\tDelete last element of List A.\
n";cout << "9\tPrint lists in ascending order.\
n";cout << "10\tPrint lists in descending
order.\n";cout << "0\tExit program\n\n";
cin >> choice;}
template <class ItemType>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";
elsecout << "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";
elsecout << item << " was not in the list.
List has not been changed.\n";}
template <class ItemType>void Retrieve(SortedType<ItemType> list){
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";}
// 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; }}
template <class ItemType>SortedType<ItemType>::SortedType(const SortedType& source){
//initialize variables length = 0; currentPos = NULL; listData = NULL;
listRear = NULL;
NodeType<ItemType>* sourcePtr; NodeType<ItemType>* newNodePtr;
//copy nodes from rhs sourcePtr = source.listData; while (sourcePtr != NULL) { newNodePtr = new NodeType<ItemType>; newNodePtr->info = sourcePtr->info; if (listData == NULL) //first node to be copied { listData = newNodePtr;
listRear = newNodePtr; newNodePtr->next = NULL;
newNodePtr->back = NULL; } else { listRear->next = newNodePtr;
newNodePtr->back = listRear; newNodePtr->next = NULL;
listRear = newNodePtr; } length++; sourcePtr = sourcePtr->next; }}
template <class ItemType>SortedType<ItemType> SortedType<ItemType>::operator =(const SortedType& rhs){
//check x=x caseif (this == &rhs)
return(*this);
//delete existing list before assigning it the values in rtList NodeType<ItemType>* lhsPtr;
NodeType<ItemType>* rhsPtr;
while (listData != NULL) { lhsPtr = listData; listData = listData->next; delete lhsPtr; } length = 0;
listRear = NULL;
//copy nodes from rhs rhsPtr = rhs.listData;
while (rhsPtr != NULL) {
lhsPtr = new NodeType<ItemType>; lhsPtr->info = rhsPtr->info; if (listData == NULL) //first node to be copied { listData = lhsPtr;
listRear = lhsPtr; lhsPtr->next = NULL;
lhsPtr->back = NULL; } else { listRear->next = lhsPtr;
lhsPtr->back = listRear; lhsPtr->next = NULL; listRear = lhsPtr; } length++; rhsPtr = rhsPtr->next; }
return (*this);}
template <class ItemType>bool SortedType<ItemType>::IsFull() const{ NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>; if (ptr == NULL) return true; else { delete ptr;
return false; }}
template <class ItemType>int SortedType<ItemType>::LengthIs() const{ return length;}
template <class ItemType>void SortedType<ItemType>::MakeEmpty(){
NodeType<ItemType>* tempPtr;
while (listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; }
listRear = NULL;length = 0;
}
template <class ItemType>void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found){ bool moreToSearch; NodeType<ItemType>* location;
location = listData; 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 insertionNodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;if (newNode == NULL)
return false;
newNode->info = item;
//check if inserting into empty listif (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
itemelse if (item < listData->info){
newNode->next = listData;newNode->back = NULL;listData->back = newNode;listData = newNode;
}//check if inserting after last itemelse 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 newNodeNodeType<ItemType>* location; location = listData;
while (location->next->info < item)location = location->next;
if(location->next->info == item)return false;
else {
//insert node into list.newNode->back = location;newNode->next = location->next;location->next->back = newNode;location->next = newNode;
}}
length++;return true;
}
template <class ItemType>void SortedType<ItemType>::RemoveFirst(){
if(listData == NULL)return;
NodeType<ItemType>* tempPtr = listData;if(listData->next == NULL){
delete tempPtr;listData = NULL;listRear = NULL;length = 0;
}else {
listData = listData->next;
listData->back = NULL;delete tempPtr;length--;
}}
template <class ItemType>void SortedType<ItemType>::RemoveLast(){
if(listData == NULL)return;
NodeType<ItemType>* tempPtr = listRear;if(listData->next == NULL){
delete tempPtr;listData = NULL;listRear = NULL;length = 0;
}else{
listRear = listRear->back;listRear->next = NULL;delete tempPtr;length--;
}}
template <class ItemType>bool SortedType<ItemType>::DeleteItem(ItemType item){
if(listData == NULL)return false;
NodeType<ItemType>* tempPtr = listData;if(item == listData->info){
if(listData->next == NULL){
delete tempPtr;listData = NULL;listRear = NULL;length = 0;
}else{
listData = listData->next;listData->back = NULL;delete tempPtr;length--;
}}else if(item == listRear->info){
tempPtr =listRear;listRear =listRear->back;listRear->next = NULL;delete tempPtr;length--;
}else{
while(tempPtr->next->info != item){
if(tempPtr->next->info > item)return false;
elsetempPtr = tempPtr->next;
}NodeType<ItemType>* newNode;newNode = tempPtr->next;tempPtr->next = newNode->next;newNode->next->back = tempPtr;delete newNode;length--;
}return true;
}
template <class ItemType>void SortedType<ItemType>::ResetList(){ currentPos = NULL;} template <class ItemType>void SortedType<ItemType>::ResetListBackward(){
currentRev = NULL;}
template <class ItemType>void SortedType<ItemType>::GetNextItem(ItemType& item){ if (currentPos == NULL) currentPos = listData; else currentPos = currentPos->next;
item = currentPos->info;}
template <class ItemType>void SortedType<ItemType>::GetPreviousItem(ItemType& item){
if (currentRev == NULL)currentRev = listRear;
elsecurrentRev = currentRev->back;
item = currentRev->info;}
// 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)
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
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(); // 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