Directory:Derek Elder/Programs/LinkedListPhoneBook
MyWikiBiz, Author Your Legacy — Friday November 29, 2024
Jump to navigationJump to searchPhoneBook.h
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <afxwin.h> using namespace std; #define new DEBUG_NEW struct PhoneBookItem { string m_name; int m_age; string m_phone; PhoneBookItem* m_next; PhoneBookItem(const string& name, int age, const string& phone); //Purpose:Default Constructor //Precondition:None //Postcondition:Data is initialized. }; ostream& operator<<(ostream& os, const PhoneBookItem& phonebooklist); class PhoneBook { friend ostream& operator<<(ostream& os, const PhoneBook& phonebook); public: PhoneBook(); //Purpose:Default Constructor //Precondition:None //Postcondition:Data is initialized. PhoneBook(const PhoneBook& pb); //Purpose:Constructor //Precondition:None //Postcondition:Data is initialized. ~PhoneBook(); //Purpose:Deconstructor //Precondition:None //Postcondition:Data is erased. PhoneBook& operator=(const PhoneBook& pb); //Purpose:Overload the = operator. //Precondition:None //Postcondition:Copy function is called and the contents of the first phonebook are copied into the second phonebook. bool IsEmpty() const; //Purpose:Checks to see if the list is empty. //Precondition:None //Postcondition:1.Return true is the list is empty. // 2.Return false if the list contains data. int Size() const; //Purpose:Checks the size of the list. //Precondition:None //Postcondition:Returns the size of the list. bool Insert(const string& name, int age, const string& phone); //modify for trailer pointer //Purpose:To insert a person's name, age, and phone number into the phone book. //Precondition:None //Postcondition:1.Returns true if the insert is successful. // 2.Returns false if the insert is not successful. bool Delete(const string& name); //Purpose:To delete a person's name, age, and phone number into the phone book. //Precondition: //Postcondition:1.Returns true if the delete is successful. // 2.Returns false if the idelete is not successful. bool Lookup(const string& name, int age, string& phone) const; //int&age &phone //Purpose:To search for a specific person in the phone book. //Precondition:None //Postcondition:1.Returns true if the search is successful and the phonebook contains data. // 2.Returns false if the search is not successful and the given item is not found. private: void Clear(); //Purpose:Clears the phonebook of all data. //Precondition:None //Postcondition:The data in the given phonebook is erased. void Copy(const PhoneBook& pb2); //Purpose:Copies a given phonebook's data into another phonebook. //Precondition:None //Postcondition:The second phonebook contains the data of the first phonebook. PhoneBookItem* m_head; int m_num; };
PhoneBook.cpp
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include "PhoneBook.h" using namespace std; PhoneBookItem::PhoneBookItem(const string& name, int age, const string& phone) :m_name(name), m_age(age), m_phone(phone), m_next(NULL) {} ostream& operator<<(ostream& os, const PhoneBookItem& pbList) { os<<"Name: "<<pbList.m_name<<"\t\tAge: "<<pbList.m_age<<"\t\tPhone Number: "<<pbList.m_phone<<endl; return os; } ostream& operator<<(ostream& os, const PhoneBook& pb) { PhoneBookItem *p = pb.m_head->m_next; if(pb.Size() == 0) { os<<"The phonebook is empty."<<endl; return os; } else { while(p != 0) { os<<*p; p = p->m_next; } return os; } } PhoneBook& PhoneBook::operator=(const PhoneBook& pb) { if(this != &pb) { Clear(); Copy(pb); } return *this; } PhoneBook::PhoneBook() :m_head(new PhoneBookItem("",-99,"")), m_num(0) {} PhoneBook::PhoneBook(const PhoneBook& pb) { m_head = NULL; m_num = 0; Copy(pb); } PhoneBook::~PhoneBook() { Clear(); } bool PhoneBook::IsEmpty() const { if(m_num == 0) return true; else return false; } int PhoneBook::Size() const { return m_num; } bool PhoneBook::Insert(const string& name, int age, const string& phone) { PhoneBookItem* temp = m_head; int tempAge = 0; string tempPhone = ""; int counter = 0; if(Size() >= 0) { if(Lookup(name,tempAge,tempPhone)) { return false; } while((counter < Size()) && ((temp->m_next)->m_name < name)) { temp = temp->m_next; counter++; } PhoneBookItem* pb2 = new PhoneBookItem(name,age,phone); pb2->m_next = temp->m_next; temp->m_next = pb2; m_num++; } return true; } bool PhoneBook::Delete(const string& name) { PhoneBookItem *p = m_head; while(p->m_next != 0) { if(p->m_next->m_name == name) { PhoneBookItem* removeIt = p->m_next; p->m_next = p->m_next->m_next; delete removeIt; m_num--; return true; } p = p->m_next; } return false; } bool PhoneBook::Lookup(const string& name, int age, string& phone) const { PhoneBookItem *p = m_head->m_next; while((p != 0) && (p->m_name <= name)) { if(p->m_name == name) { age = p->m_age; phone = p->m_phone; return true; } p = p->m_next; } return false; } void PhoneBook::Clear() { PhoneBookItem* p = m_head; PhoneBookItem* temp; while(p != 0) { temp = p; p = p->m_next; delete temp; } } void PhoneBook::Copy(const PhoneBook& pb2) { PhoneBookItem* p2 = pb2.m_head->m_next; PhoneBookItem* pThis = new PhoneBookItem("",-99,""); m_head = pThis; while(p2 != 0) { pThis->m_next = new PhoneBookItem(p2->m_name,p2->m_age,p2->m_phone); p2 = p2->m_next; pThis = pThis->m_next; } m_num = pb2.m_num; }
Main.cpp
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include "PhoneBook.h" using namespace std; void clearScreen(); //Purpose:To clear the screen. //Precondition:None //Postcondition:Screen is cleared. char displayMenuAndGetSelection(); //Purpose:To display the menu and get the selection for searching. //Precondition:None //Postcondition:Search choice is entered and appropriate function is called. void Pause(); //Purpose:To pause the program and allow the user a break. //Precondition:None //Postcondition:None void quitProgram(); //Purpose:To inform the user the program has been terminated. //Precondition:None //Postcondition:Program terminated. void doLookup(PhoneBook& pb); //Purpose:doLookup function calls Lookup function to find a person from the phonebook. //Precondition:Choice must be selected from the menu. //Postcondition:Lookup is performed and results are displayed. void doInsert(PhoneBook& pb); //Purpose:doInsert function calls Insert function to insert a person into the phonebook. //Precondition:Choice must be selected from the menu. //Postcondition:Insert is performed and results are displayed. void doDelete(PhoneBook& pb); //Purpose:doDelete function calls Delete function to delete a person from the phonebook. //Precondition:Choice must be selected from the menu. //Postcondition:Delete is performed and results are displayed. void main() { PhoneBook pb1; bool done = false; cout<<"Before any people are inserted into the phonebook."<<endl; cout<<pb1; //Should print out no one is present. //Initial inserts pb1.Insert("Joe",23,"11234567890"); pb1.Insert("Marv",40,"15555555555"); pb1.Insert("Larry",33,"12121212121"); pb1.Insert("Kenrick",15,"18001314554"); pb1.Insert("Joe",26,"19375530293"); //Should not insert because a //person with the name of Joe is already present. cout<<"=======PhoneBook 1======="<<endl; //overload << operator cout<<pb1; cout<<"=======PhoneBook 2======="<<endl; //call copy function PhoneBook pb2(pb1); pb2.Delete("Joe"); //deletes Joe out of pb2 so only three names should appear. cout<<pb2; cout<<"=======PhoneBook 3======="<<endl; PhoneBook pb3; //overload = operator pb3 = pb1; cout<<pb3; Pause(); while(!done) { char menuChoice = ' '; menuChoice = displayMenuAndGetSelection(); clearScreen(); switch(menuChoice) { case '1': cout<<pb1; Pause(); done = false; break; case '2': doInsert(pb1); Pause(); done = false; break; case '3': doDelete(pb1); Pause(); done = false; break; case '4': doLookup(pb1); Pause(); done = false; break; case '5': cout<<"There are currently "<<pb1.Size()<<" people in the phonebook."<<endl; Pause(); done = false; break; case '6': quitProgram(); done = true; break; default: cout<<"Incorrect choice selected"<<endl; Pause(); done = false; break; } } } void clearScreen() { system("cls"); } char displayMenuAndGetSelection() { char choice; clearScreen(); cout<<endl<<endl<<endl; cout<<"'1' -- View the Phonebook."<<endl<<endl; cout<<"'2' -- Insert a new person's information into the phonebook."<<endl<<endl; cout<<"'3' -- Delete a person's information from the phonebook."<<endl<<endl; cout<<"'4' -- Search the phonebook."<<endl<<endl; cout<<"'5' -- Count the number of people in the phonebook."<<endl<<endl; cout<<"'6' -- Quit the program."<<endl<<endl; cin>>choice; cin.ignore(50,'\n'); return choice; } void Pause() { cout<<endl<<"Press 'ENTER' to continue..."<<endl; cin.get(); } void quitProgram() { cout<<"Program terminated, good bye!"<<endl; } void doLookup(PhoneBook& pb) { string name,phone; cout<<"What person would you like to search for? "; cin>>name; cin.ignore(50,'\n'); pb.Lookup(name,0,phone); if((pb.Lookup(name,0,phone)) == true) { cout<<endl<<"The person you have searched for is in the phonebook."<<endl; cout<<"The phone number of the person is: "<<phone<<endl; } else { cout<<endl<<"The person you have searched for is not in the phonebook."<<endl; } } void doInsert(PhoneBook& pb) { string name, phone; int age; cout<<"Please enter the relevant information to be entered into the phonebook."<<endl; cout<<"Name: "; cin>>name; cout<<"Age: "; cin>>age; cout<<"Phone Number: "; cin>>phone; cin.ignore(50,'\n'); pb.Insert(name,age,phone); cout<<endl<<"Insert has been performed."<<endl; } void doDelete(PhoneBook& pb) { string name; cout<<"What person would you like to delete from the phonebook? "; cin>>name; cin.ignore(50,'\n'); pb.Delete(name); cout<<endl<<"Delete has been performed."<<endl; }