In: Computer Science
8.13 LAB: Warm up: Contacts
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(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
/* Type code here */
_________________
ContactNode.h
/* Type code here */
CONTACT NODE .H FILE
#ifndef CONTACTNODE_H
#define CONTACTNODE_H
#include<iostream>
#include<string>
using namespace std;
class ContactNode
{
string ContactName;
string ContactPhoneNum;
ContactNode*nextNodeptr;
public:
ContactNode(string,string);
ContactNode*InsertAfter(ContactNode*,string,string,ContactNode*);
string GetName(ContactNode*);
string GetPhone(ContactNode*);
void printContactNode(ContactNode*);
};
#endif // CONTACTNODE_H
CONTACTNODE .CPP FILE
#include "ContactNode.h"
#include<iostream>
#include<string>
using namespace std;
ContactNode::ContactNode(string name,string phone) ///Parametrized Constructor
{
this->ContactName=name;
this->ContactPhoneNum=phone;
this->nextNodeptr=NULL;
}
ContactNode* ContactNode::InsertAfter(ContactNode*head,string name,string phone,ContactNode*tail) ///Inserting Node
{
if(head==NULL)
{
ContactNode*node=new ContactNode(name,phone);
head=node;
tail=head;
}
else{
ContactNode*ptr=head;
ContactNode*node=new ContactNode(name,phone);
while(ptr->nextNodeptr!=NULL)
ptr=ptr->nextNodeptr;
ptr->nextNodeptr=node;
tail=ptr->nextNodeptr;
}
return head;
}
string ContactNode::GetName(ContactNode*node) ///Function returning name of node
{
return node->ContactName;
}
string ContactNode::GetPhone(ContactNode*node) ///Function returning phone of node
{
return node->ContactPhoneNum;
}
void ContactNode::printContactNode(ContactNode*head) ///Function Printing Complete Node
{
ContactNode*ptr=head;
while(ptr!=NULL)
{
cout<<"Name: "<<GetName(ptr)<<endl;
cout<<"Phone Number: "<<GetPhone(ptr)<<endl<<endl;
ptr=ptr->nextNodeptr;
}
}
///////////////****************MAIN FUNCTION!!!*****************
int main()
{
ContactNode obj("","");
ContactNode *head=NULL,*tail=NULL;
for(int i=0;i<3;i++)
{
string name,phone;
cout<<"Person "<<i+1<<endl;
cout<<"Enter Name: ";
getline(cin,name);
cout<<"Enter Phone number: ";
getline(cin,phone);
cout<<"You entered : "<<name<<", "<<phone<<endl<<endl;
head=obj.InsertAfter(head,name,phone,tail);
}
cout<<"\nCONTACT LIST\n";
obj.printContactNode(head);
}
Above is code snippet in C++ performing all the task mentioned in question.
Two different files are used. Please keep in mind they must be in same folder and should be saved with same name as mentioned in question.
OUTPUT'S SCREENSHOT