In: Computer Science
(1) Create three files to submit.
(2) Build the ContactNode class per the following specifications:
Parameterized constructor. Parameters are name followed by phone number.
Public member functions
Private data members
Ex. of PrintContactNode() output:
Name: Roxanne Hughes Phone number: 443-555-2864
(3) In main(), prompt the user for three contacts and output the
user's input. Create three ContactNodes and use the nodes to build
a linked list. (2 pts)
Ex:
Person 1 Enter name: Roxanne Hughes Enter phone number: 443-555-2864 You entered: Roxanne Hughes, 443-555-2864 Person 2 Enter name: Juan Alberto Jr. Enter phone number: 410-555-9385 You entered: Juan Alberto Jr., 410-555-9385 Person 3 Enter name: Rachel Phillips Enter phone number: 310-555-6610 You entered: Rachel Phillips, 310-555-6610
(4) Output the linked list. (2 pts)
Ex:
CONTACT LIST Name: Roxanne Hughes Phone number: 443-555-2864 Name: Juan Alberto Jr. Phone number: 410-555-9385 Name: Rachel Phillips Phone number: 310-555-6610
main.cpp
#include <iostream>
using namespace std;
int main() {
/* Type your code here. */
return 0;
}
ContactNode.cpp
ContactNode.h
How do you write this code?
SOLUTION-
I have solve the problem in C++ code with comments and screenshot
for easy understanding :)
CODE-
ContactNode.h file
#ifndef CONTACTNODE_H
#define CONTACTNODE_H
using namespace std;
class ContactNode{
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
public:
ContactNode();
ContactNode(string name, string phone);
void InsertAfter(ContactNode*);
string GetName() const;
string GetPhoneNumber() const;
ContactNode* GetNext();
void PrintContactNode();
};
#endif
ContactNode.cpp file:
#include <iostream>
#include "ContactNode.h"
using namespace std;
/**
* Default constructor
* Assigns nextNodePtr to NULL
*/
ContactNode::ContactNode(){
nextNodePtr = NULL;
}
/**
* Parameterized constructor
* @param name a string represents contact name
* @param phone a string represents phone number
*/
ContactNode::ContactNode(string name, string phone){
contactName = name;
contactPhoneNum = phone;
nextNodePtr=NULL;
}
/**
* Method Name: InsertAfter
* Makes connection between this node and the nextnode
*/
void ContactNode::InsertAfter(ContactNode *nextNode){
//If the next pointer is null just assign the nextnode
if(nextNodePtr == NULL)
nextNodePtr=nextNode;
else{
//Define a temp node to store current next pointer
ContactNode *temp = nextNodePtr;
//Then loop through the nextnodes and adjust the pointers
while(temp->nextNodePtr != NULL){
temp = temp->GetNext();
}
//Assign the nextnode to temp node
temp->nextNodePtr = nextNode;
}
}
/** Getter for Name **/
string ContactNode::GetName() const{
return contactName;
}
/** Getter for Phone number **/
string ContactNode::GetPhoneNumber() const{
return contactPhoneNum;
}
/** Getter for next pointer **/
ContactNode* ContactNode::GetNext(){
return nextNodePtr;
}
/**
* Print contact Node - Name and Phone
*/
void ContactNode::PrintContactNode(){
//Go through the next pointer and display name and
phonenumber
if(nextNodePtr != NULL) {
cout << "Name: " << nextNodePtr->GetName() <<
endl;
cout << "Phone number: " <<
nextNodePtr->GetPhoneNumber() << endl;
cout << endl;
GetNext()->PrintContactNode();
}
}
main.cpp file:
#include <iostream>
#include "ContactNode.h"
using namespace std;
//Define a constant for total number of
contacts
int TOTAL_CONTACTS = 3;
int main()
{
//Declare two nodes to keep track of both the head and tail
nodes
ContactNode *head, *tail;
head = new ContactNode;
//Get the contact node for TOTAL_CONTACTS times
for(int i=0; i < TOTAL_CONTACTS; i++){
//Display headings
cout << "\nPerson " << i+1 <<endl;
//Get Person name
cout<<"Enter name: "<<endl ;
string name;
getline(cin, name);
//Get Phone number
cout<<"Enter phone number: "<<endl ;
string phonenum;
getline(cin, phonenum);
//Display enetered values
cout << "You entered: "<< name << ", " <<
phonenum << endl;
//Create a node
tail = new ContactNode(name, phonenum);
//Insert after the head
head->InsertAfter(tail);
}
cout << endl;
cout << "CONTACT LIST" << endl;
//Print contacts from head
head->PrintContactNode();
return 0;
}
SCREENSHOT-
main.cpp file:
ContactNode.h file
ContactNode.cpp file:
Sample Run:
IF YOU HAVE ANY DOUBT
PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------