c++ detyrat postim_slideshare

28
//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

Upload: tctal

Post on 14-Apr-2017

144 views

Category:

Education


0 download

TRANSCRIPT

Page 1: 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

Page 2: C++ detyrat postim_slideshare

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(){

Page 3: C++ detyrat postim_slideshare

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;

Page 4: C++ detyrat postim_slideshare

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){

Page 5: C++ detyrat postim_slideshare

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++)

Page 6: C++ detyrat postim_slideshare

{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;}

Page 7: C++ detyrat postim_slideshare

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";}

Page 8: C++ detyrat postim_slideshare

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";}

Page 9: C++ detyrat postim_slideshare

// 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; }}

Page 10: C++ detyrat postim_slideshare

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;

Page 11: C++ detyrat postim_slideshare

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) {

Page 12: C++ detyrat postim_slideshare

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;

Page 13: C++ detyrat postim_slideshare

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;

Page 14: C++ detyrat postim_slideshare

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;

Page 15: C++ detyrat postim_slideshare

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;

Page 16: C++ detyrat postim_slideshare

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;

Page 17: C++ detyrat postim_slideshare

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){

Page 18: C++ detyrat postim_slideshare

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;

Page 19: C++ detyrat postim_slideshare

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;

Page 20: C++ detyrat postim_slideshare

item = currentPos->info;}

template <class ItemType>void SortedType<ItemType>::GetPreviousItem(ItemType& item){

if (currentRev == NULL)currentRev = listRear;

elsecurrentRev = currentRev->back;

item = currentRev->info;}

Page 21: C++ detyrat postim_slideshare

// 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;

Page 22: C++ detyrat postim_slideshare

// 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

Page 23: C++ detyrat postim_slideshare

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.

Page 24: C++ detyrat postim_slideshare

// 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"

Page 25: C++ detyrat postim_slideshare

#endif