Using C++, use a linked list to create an alphabetical Contact Book to store the names, addresses,
Question:
Using C++, use a linked list to create an alphabetical Contact Book to store the names, addresses, and phone numbers of our contacts. The data structure used contain contacts should be a linked list and each time a new contact is added the contact will be inserted into the correct alphabetical location by last name. Assume there are no contacts that have the same last name. There will be two classes. One named LinkedList and the other named PersonNode. I have attached the LinkedList classes and the PersonNode.h. I need the PersonNode.cpp and main class to get the program running. Please follow the instructions provided.
The LinkedList class will be used to create a single LinkedList object.
- The class will have two PersonNode pointer fields
*headPtr that will maintain the head of the linked list
*tailPtr that will maintain the tail of the linked list
- The class will have the following functions::
*a constructor that initialized the headPtr and tailPtr to NULL
*addLink that will take a PersonNode reference or pointer and add the node to the linked list
*findInsertSpot that will return a PersonNode pointer that points to the location where a PersonNode is to be inserted into the linked list
*getHeadPtr that will return the headPtr
The PersonNode class will be used to create the nodes that are part of the linked list
- The class will have the following fields:
*fName - string contains the contacts first name
*lName - string contains the contact last name
*address - string contains the contacts address
*phone - string contains the contacts phone number
*next - PersonNode pointer that points to the next contact
- The class will contain all of the getters and setters for the above named fields (see class diagram below) in addition to
*getFullName - returns the string of lName, fName
*a constructor that takes no arguments and sets next to NULL
*a constructor that takes arguments to set fName, lName, address, and phone and sets next to NULL
- Finally, create the ability to search for a PersonNode entry from the Contact Book and remove a PersonNode entry from the Contact Book.
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "PersonNode.h"
class LinkedList {
private:
PersonNode* headPtr;
PersonNode* tailPtr;
public:
LinkedList();
void addLink(PersonNode& newPerson);
PersonNode* findInsertSpot(PersonNode& newPerson);
PersonNode* getHeadPtr();
void removeLink(PersonNode& targetPerson);
PersonNode* findLink(std::string lastName);
};
#endif
LinkedList.cpp
#include "LinkedList.h"
#include
LinkedList::LinkedList() {
headPtr = nullptr;
tailPtr = nullptr;
}
void LinkedList::addLink(PersonNode& newPerson) {
PersonNode* insertSpot = findInsertSpot(newPerson);
if (insertSpot == nullptr) {
headPtr = &newPerson;
}
else {
newPerson.setNext(insertSpot->getNext());
insertSpot->setNext(&newPerson);
}
if (newPerson.getNext() == nullptr) {
tailPtr = &newPerson;
}
}
PersonNode* LinkedList::findInsertSpot(PersonNode& newPerson) {
PersonNode* currentPerson = headPtr;
PersonNode* previousPerson = nullptr;
while (currentPerson != nullptr && currentPerson->getFullName() < newPerson.getFullName()) {
previousPerson = currentPerson;
currentPerson = currentPerson->getNext();
}
return previousPerson;
}
PersonNode* LinkedList::getHeadPtr() {
return headPtr;
}
void LinkedList::removeLink(PersonNode& targetPerson) {
PersonNode* currentPerson = headPtr;
PersonNode* previousPerson = nullptr;
while (currentPerson != nullptr && currentPerson != &targetPerson) {
previousPerson = currentPerson;
currentPerson = currentPerson->getNext();
}
if (currentPerson == nullptr) {
std::cout << "Error: " << targetPerson.getFullName() << " not found." << std::endl;
}
else {
if (currentPerson == headPtr) {
headPtr = currentPerson->getNext();
}
else {
previousPerson->setNext(currentPerson->getNext());
}
if (currentPerson == tailPtr) {
tailPtr = previousPerson;
}
}
}
PersonNode* LinkedList::findLink(std::string lastName) {
PersonNode* currentPerson = headPtr;
while (currentPerson != nullptr) {
if (currentPerson->getLastName() == lastName) {
return currentPerson;
}
currentPerson = currentPerson->getNext();
}
std::cout << "Error: " << lastName << " not found." << std::endl;
return nullptr;
}
PersonNode.h
#ifndef PERSONNODE_H
#define PERSONNODE_H
#include
class PersonNode {
private:
std::string fName;
std::string lName;
std::string address;
std::string phone;
PersonNode* next;
public:
PersonNode();
PersonNode(std::string fName, std::string lName, std::string address, std::string phone);
std::string getFullName();
std::string getFirstName();
std::string getLastName();
std::string getAddress();
std::string getPhone();
PersonNode* getNext();
void setFirstName(std::string newFName);
void setLastName(std::string newLName);
void setAddress(std::string newAddress
); void setPhone(std::string newPhone); void setNext(PersonNode* newNext);
};
#endif
Financial Accounting
ISBN: 978-0078025549
3rd edition
Authors: J. David Spiceland, Wayne Thomas, Don Herrmann