Question: This is C++. Please submit all file of code with file names. Revise the Chapter 11 Composition example so that it uses a linked list
This is C++. Please submit all file of code with file names.
Revise the Chapter 11 Composition example so that it uses a linked list to hold the collection of address book entries.
Create a project and copy over the files from the Chapter 11 Composition example as well as the linkedList.h and orderedLinkedList.h files from the Chapter 16/Source Code from Text folder.
Modify the extPersonType class to
Overload the >= and == operators so that they return true if the last names of the operands are the same.
Modify the addressBookType class to
Inherit from orderedLinkedList. The addressBookType class does not have to be generic, fix the type of the items in orderedLinkedList to be extPersonType.
Modify addEntry() and findPerson() to use the linked list operations.
Extend the addressBookType class by adding these operations:
addPerson() to get the information interactively for a person and add an entry for them to the address book.
removePerson() to delete a specific person's entry from the address book.
readInfo() which takes a file name as an input parameter and reads the address book information from that file (a data file is provided for testing).
saveInfo() which takes a file name as an input parameter and writes the address book information back out to that file.
Write a program to manage the address book. The program should read the address book information from a file when it starts and write the information back out to a file when it terminates.
Make your program menu-driven and use this menu:
Select an option to manage your address book:
Find a person
Add a new person
Remove a person
Print the address book
Quit
Do not make changes to any classes other than extPersonType and addressBookType.
Turn in all your class files, your test program, your data file after testing, and one or more screen shots showing the results of your testing.
Addresses....
Shelly Malik Lincoln Drive Hampton VA 23606 757-555-1212 Donald Duck Disney Street Orlando FL 11234 622-873-8920 Chelsea Tomak Kennedy Blvd Newport News VA 23602 757-777-8888 Goof Goofy Disney Street Los Angles CA 91340 215-782-9000 Brave Balto Disney Road Orlando FL 35672 415-782-5555 Bash Bashfull Long Road New York NY 01101 212-782-8000
***************************linkedList.h **********************************
#ifndef H_LinkedListType #define H_LinkedListType #include#include using namespace std; //Definition of the node template struct nodeType { Type info; nodeType *link; }; template class linkedListIterator { public: linkedListIterator(); //Default constructor //Postcondition: current = nullptr; linkedListIterator(nodeType *ptr); //Constructor with a parameter. //Postcondition: current = ptr; Type operator*(); //Function to overload the dereferencing operator *. //Postcondition: Returns the info contained in the node. linkedListIterator operator++(); //Overload the pre-increment operator. //Postcondition: The iterator is advanced to the next // node. bool operator==(const linkedListIterator & right) const; //Overload the equality operator. //Postcondition: Returns true if this iterator is equal to // the iterator specified by right, // otherwise it returns the value false. bool operator!=(const linkedListIterator & right) const; //Overload the not equal to operator. //Postcondition: Returns true if this iterator is not // equal to the iterator specified by // right; otherwise it returns the value // false. private: nodeType *current; //pointer to point to the current //node in the linked list }; template linkedListIterator ::linkedListIterator() { current = nullptr; } template linkedListIterator :: linkedListIterator(nodeType *ptr) { current = ptr; } template Type linkedListIterator ::operator*() { return current->info; } template linkedListIterator linkedListIterator :: operator++() { current = current->link; return *this; } template bool linkedListIterator ::operator== (const linkedListIterator & right) const { return (current == right.current); } template bool linkedListIterator ::operator!= (const linkedListIterator & right) const { return (current != right.current); } //***************** class linkedListType **************** template class linkedListType { public: const linkedListType & operator= (const linkedListType &); //Overload the assignment operator. void initializeList(); //Initialize the list to an empty state. //Postcondition: first = nullptr, last = nullptr, // count = 0; bool isEmptyList() const; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print() const; //Function to output the data contained in each node. //Postcondition: none int length() const; //Function to return the number of nodes in the list. //Postcondition: The value of count is returned. void destroyList(); //Function to delete all the nodes from the list. //Postcondition: first = nullptr, last = nullptr, // count = 0; Type front() const; //Function to return the first element of the list. //Precondition: The list must exist and must not be // empty. //Postcondition: If the list is empty, the program // terminates; otherwise, the first // element of the list is returned. Type back() const; //Function to return the last element of the list. //Precondition: The list must exist and must not be // empty. //Postcondition: If the list is empty, the program // terminates; otherwise, the last // element of the list is returned. virtual bool search(const Type& searchItem) const = 0; //Function to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is in the // list, otherwise the value false is // returned. virtual void insertFirst(const Type& newItem) = 0; //Function to insert newItem at the beginning of the list. //Postcondition: first points to the new list, newItem is // inserted at the beginning of the list, // last points to the last node in the list, // and count is incremented by 1. virtual void insertLast(const Type& newItem) = 0; //Function to insert newItem at the end of the list. //Postcondition: first points to the new list, newItem // is inserted at the end of the list, // last points to the last node in the // list, and count is incremented by 1. virtual void deleteNode(const Type& deleteItem) = 0; //Function to delete deleteItem from the list. //Postcondition: If found, the node containing // deleteItem is deleted from the list. // first points to the first node, last // points to the last node of the updated // list, and count is decremented by 1. linkedListIterator begin(); //Function to return an iterator at the begining of //the linked list. //Postcondition: Returns an iterator such that current // is set to first. linkedListIterator end(); //Function to return an iterator one element past the //last element of the linked list. //Postcondition: Returns an iterator such that current // is set to nullptr. linkedListType(); //Default constructor //Initializes the list to an empty state. //Postcondition: first = nullptr, last = nullptr, // count = 0; linkedListType(const linkedListType & otherList); //copy constructor ~linkedListType(); //Destructor //Deletes all the nodes from the list. //Postcondition: The list object is destroyed. protected: int count; //variable to store the number of //elements in the list nodeType *first; //pointer to the first node of the list nodeType *last; //pointer to the last node of the list private: void copyList(const linkedListType & otherList); //Function to make a copy of otherList. //Postcondition: A copy of otherList is created and // assigned to this list. }; template bool linkedListType ::isEmptyList() const { return (first == nullptr); } template linkedListType ::linkedListType() //default constructor { first = nullptr; last = nullptr; count = 0; } template void linkedListType ::destroyList() { nodeType *temp; //pointer to deallocate the memory //occupied by the node while (first != nullptr) //while there are nodes in { //the list temp = first; //set temp to the current node first = first->link; //advance first to the next node delete temp; //deallocate the memory occupied by temp } last = nullptr; //initialize last to nullptr; first has //already been set to nullptr by the while loop count = 0; } template void linkedListType ::initializeList() { destroyList(); //if the list has any nodes, delete them } template void linkedListType ::print() const { nodeType *current; //pointer to traverse the list current = first; //set current so that it points to //the first node while (current != nullptr) //while more data to print { cout << current->info << " "; current = current->link; } }//end print template int linkedListType ::length() const { return count; } //end length template Type linkedListType ::front() const { assert(first != nullptr); return first->info; //return the info of the first node }//end front template Type linkedListType ::back() const { assert(last != nullptr); return last->info; //return the info of the last node }//end back template linkedListIterator linkedListType ::begin() { linkedListIterator temp(first); return temp; } template linkedListIterator linkedListType ::end() { linkedListIterator temp(nullptr); return temp; } template void linkedListType ::copyList (const linkedListType & otherList) { nodeType *newNode; //pointer to create a node nodeType *current; //pointer to traverse the list if (first != nullptr) //if the list is nonempty, make it empty destroyList(); if (otherList.first == nullptr) //otherList is empty { first = nullptr; last = nullptr; count = 0; } else { current = otherList.first; //current points to the //list to be copied count = otherList.count; //copy the first node first = new nodeType ; //create the node first->info = current->info; //copy the info first->link = nullptr; //set the link field of //the node to nullptr last = first; //make last point to the //first node current = current->link; //make current point to //the next node //copy the remaining list while (current != nullptr) { newNode = new nodeType ; //create a node newNode->info = current->info; //copy the info newNode->link = nullptr; //set the link of //newNode to nullptr last->link = newNode; //attach newNode after last last = newNode; //make last point to //the actual last node current = current->link; //make current point //to the next node }//end while }//end else }//end copyList template linkedListType ::~linkedListType() //destructor { destroyList(); }//end destructor template linkedListType ::linkedListType (const linkedListType & otherList) { first = nullptr; copyList(otherList); }//end copy constructor //overload the assignment operator template const linkedListType & linkedListType ::operator= (const linkedListType & otherList) { if (this != &otherList) //avoid self-copy { copyList(otherList); }//end else return *this; } #endif
********************************************end********************************************************
**********************************************orderedLinkedList.h****************************************************8
#ifndef H_orderedListType #define H_orderedListType #include "linkedList.h" using namespace std; templateclass orderedLinkedList: public linkedListType { public: bool search(const Type& searchItem) const; //Function to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is in the // list, otherwise it returns false. void insert(const Type& newItem); //Function to insert newItem in the list. //Postcondition: first points to the new list, newItem // is inserted at the proper place in the // list, and count is incremented by 1. void insertFirst(const Type& newItem); //Function to insert newItem in the list. //Because the resulting list must be sorted, newItem is //inserted at the proper in the list. //This function uses the function insert to insert newItem. //Postcondition: first points to the new list, newItem is // inserted at the proper in the list, // and count is incremented by 1. void insertLast(const Type& newItem); //Function to insert newItem in the list. //Because the resulting list must be sorted, newItem is //inserted at the proper in the list. //This function uses the function insert to insert newItem. //Postcondition: first points to the new list, newItem is // inserted at the proper in the list, // and count is incremented by 1. void deleteNode(const Type& deleteItem); //Function to delete deleteItem from the list. //Postcondition: If found, the node containing // deleteItem is deleted from the list; // first points to the first node of the // new list, and count is decremented by 1. // If deleteItem is not in the list, an // appropriate message is printed. void mergeLists(orderedLinkedList &list1, orderedLinkedList &list2); //This operation creates a new list by merging the elements //of list1 and list2. //Precondition: Both lists list1 and list2 are ordered. //Postcondition: first points to the merged list. // list1 and list2 are empty. }; template bool orderedLinkedList :: search(const Type& searchItem) const { bool found = false; nodeType *current; //pointer to traverse the list current = this->first; //start the search at the first node while (current != nullptr && !found) if (current->info >= searchItem) found = true; else current = current->link; if (found) found = (current->info == searchItem); //test for equality return found; }//end search template void orderedLinkedList ::insert(const Type& newItem) { nodeType *current; //pointer to traverse the list nodeType *trailCurrent = nullptr; //pointer just //before current nodeType *newNode; //pointer to create a node bool found; newNode = new nodeType ; //create the node newNode->info = newItem; //store newItem in the node newNode->link = nullptr; //set the link field of the node //to nullptr if (this->first == nullptr) //Case 1 { this->first = newNode; this->last = newNode; this->count++; } else { current = this->first; found = false; while (current != nullptr && !found) //search the list if (current->info >= newItem) found = true; else { trailCurrent = current; current = current->link; } if (current == this->first) //Case 2 { newNode->link = this->first; this->first = newNode; this->count++; } else //Case 3 { trailCurrent->link = newNode; newNode->link = current; if (current == nullptr) this->last = newNode; this->count++; } }//end else }//end insert template void orderedLinkedList ::insertFirst(const Type& newItem) { insert(newItem); }//end insertFirst template void orderedLinkedList ::insertLast(const Type& newItem) { insert(newItem); }//end insertLast template void orderedLinkedList ::deleteNode(const Type& deleteItem) { nodeType *current; //pointer to traverse the list nodeType *trailCurrent = nullptr; //pointer just //before current bool found; if (this->first == nullptr) //Case 1 cout << "Cannot delete from an empty list." << endl; else { current = this->first; found = false; while (current != nullptr && !found) //search the list if (current->info >= deleteItem) found = true; else { trailCurrent = current; current = current->link; } if (current == nullptr) //Case 4 cout << "The item to be deleted is not in the " << "list." << endl; else if (current->info == deleteItem) //the item to be //deleted is in the list { if (this->first == current) //Case 2 { this->first = this->first->link; if (this->first == nullptr) this->last = nullptr; delete current; } else //Case 3 { trailCurrent->link = current->link; if (current == this->last) this->last = trailCurrent; delete current; } this->count--; } else //Case 4 cout << "The item to be deleted is not in the " << "list." << endl; } }//end deleteNode template void orderedLinkedList ::mergeLists(orderedLinkedList &list1, orderedLinkedList &list2) { nodeType *lastSmall; //pointer to the last node of // the merged list. nodeType *first1 = list1.first; nodeType *first2 = list2.first; if (list1.first == nullptr) //first sublist is empty { this->first = list2.first; list2.first = nullptr; this->count = list2.count; } else if (list2.first == nullptr) // second sublist is empty { this->first = list1.first; list1.first = nullptr; this->count = list1.count; } else { if (first1->info < first2->info) //Compare first nodes { this->first = first1; first1 = first1->link; lastSmall = this->first; } else { this->first = first2; first2 = first2->link; lastSmall = this->first; } while (first1 != nullptr && first2 != nullptr) { if (first1->info < first2->info) { lastSmall->link = first1; lastSmall = lastSmall->link; first1 = first1->link; } else { lastSmall->link = first2; lastSmall = lastSmall->link; first2 = first2->link; } } //end while if (first1 == nullptr) //first sublist exhausted first { lastSmall->link = first2; this->last = first2; } else //second sublist exhausted first { lastSmall->link = first1; this->last = first1; } list1.first = nullptr; list1.last = nullptr; list2.first = nullptr; list2.last = nullptr; this->count = list1.count + list2.count; list1.count = 0; list2.count = 0; } } //end mergeList #endif
**************************************end**********************************************
***************************addressType.h*****************************************
#ifndef ADRESSTYPE_H_INCLUDED #define ADRESSTYPE_H_INCLUDED using namespace std; class addressType { public: //Getters and setters void setAddress(string); void setCity(string); void setState(string); // Preconditions: use two-letter postal abbreviations void setZipcode(int); //Preconditions: use the 5-digit format string getAddress() const; string getCity() const; string getState() const; int getZipcode() const; void print() const; //Postconditions: Address printed in following format: // address // city, state zipcode addressType(string="", string="", string="XX", int = 10000); // Parameters are: address, city, state, zipcode // Preconditions: state must contain two characters, zipcode must be between 11111 and 99999 // If parameters are not supplied, the defaults indicated above will be used // If state and zipcode are invalid, default values will be used private: string address; string city; string state; int zipcode; }; #endif // ADRESSTYPE_H_INCLUDED *********************************************end**************************************
**************************************addressType.cpp*********************************
#include#include #include "addressType.h" using namespace std; void addressType::setAddress(string add) { address = add; } void addressType::setCity(string c) { city = c; } void addressType::setState(string st) { if (st.length() == 2) state = st; else state = "XX"; } void addressType::setZipcode(int zip) { if (zip > 11111 && zip < 99999) zipcode = zip; else zipcode = 10000; } string addressType::getAddress() const { return address; } string addressType::getCity() const { return city; } string addressType::getState() const { return state; } int addressType::getZipcode() const { return zipcode; } void addressType::print() const { cout << address << endl; cout << city << " " << state << " " << zipcode << endl; } addressType::addressType(string add, string cty, string st, int zip) { address = add; city = cty; if (st.length() == 2) state = st; else state = "XX"; if (zip > 11111 && zip < 99999) zipcode = zip; else zipcode = 10000; }
***************************************************end************************************************
*************************************extPersonType.h********************************************
#ifndef EXTPERSONTYPE_H_INCLUDED #define EXTPERSONTYPE_H_INCLUDED #include "personType.h" #include "addressType.h" using namespace std; class extPersonType: public personType { public: void setPhoneNumber(string); string getPhoneNumber() const; void print() const; // Postcondition - prints the person's info as follows: // Name // address // city, state zipcode // Phone number extPersonType(string="", string="", string="", string="", string="XX", int = 10000, string=""); // Parameters: first name, last name, address, // city, state, zipcode, phone number // Preconditions: state restricted to two characters, // zipcode to 5 digits. // If parameters not supplied, values assigned using defaults shown // If state or zipcode invalid, the defaults will be used. private: addressType address; string phoneNumber; }; #endif // EXTPERSONTYPE_H_INCLUDED ************************************************end************************************************
*****************************************extPersonType.cpp************************************
#include#include "extPersonType.h" using namespace std; void extPersonType::setPhoneNumber(string number) { phoneNumber = number; } string extPersonType::getPhoneNumber() const { return phoneNumber; } void extPersonType::print() const { personType::print(); cout << endl; address.print(); cout << phoneNumber << endl; } extPersonType::extPersonType(string first, string last, string add, string cty, string st, int zip, string phone) :personType(first, last), address(add, cty, st, zip) { phoneNumber = phone; }
**************************************end***********************************
****************************************************addressBookType.h*************************************
#ifndef ADDRESSBOOKTYPE_H #define ADDRESSBOOKTYPE_H #include "extPersonType.h" class addressBookType { public: void addEntry(extPersonType); // Parameters - personType object // Preconditions - PersonType object exists and has been initialized // Postconditions - The person is added to the list and // list length adjusted // If the last slot is filled, an error message is written void findPerson(string); // Parameters - the last name of a person // Postconditions - If the person is in the list, their entry is printed void print(); // Postcondition: all the entries in the address book are printed private: extPersonType addressList[500]; int length=0; int maxLength = 500; }; #endif // ADDRESSBOOKTYPE_H #include#include #include "addressBookType.h" using namespace std; void addressBookType::addEntry(extPersonType person) { if (length < maxLength) { addressList[length] = person; length++; } else cout << "Address book is full" << endl; } void addressBookType::findPerson(string lastName) { bool found = false; for(int i=0; i < maxLength; i++) { if (addressList[i].getLastName() == lastName) { addressList[i].print(); cout << endl; found = true; break; } } if (!found) cout << lastName << " not found" << endl; } void addressBookType::print() { for (int i=0; i < length; i++) { addressList[i].print(); cout << endl; } } ****************************end*****************************
****************************************main.cpp***********************************
#include#include "addressBookType.h" using namespace std; int main() { addressBookType addressBook; string first, last, address, city, state, phone; int zip; char go = 'y'; while (go == 'y' || go == 'Y') { cout << "Enter first and last name: "; cin >> first >> last; cin.get(); cout << "Enter street address: "; getline(cin, address); cout << "Enter city: "; getline(cin, city); cout << "Enter state and zip code: "; cin >> state >> zip; cin.get(); cout << "Enter phone number: "; getline(cin, phone); extPersonType newPerson(first, last, address, city, state, zip, phone); addressBook.addEntry(newPerson); cout << "Would you like to enter another person (y/n)? "; cin >> go; } go = 'y'; while (go == 'y' || go == 'Y') { cout << "Enter the last name of the person you want to find: "; cin >> last; addressBook.findPerson(last); cout << "Would you like to find another person (y/n)? "; cin >> go; } cout << "Address Book Entries: " << endl << endl; addressBook.print(); return 0; }
******************************************************end******************************************
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
