C++ 7th edition chapter 16. Please use Linked Lists and there should be three fi
ID: 3918393 • Letter: C
Question
C++ 7th edition chapter 16. Please use Linked Lists and there should be three files. The output should ask only 3 numbers.
You will design an online contact list to keep track of names and phone numbers.
a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables.
b. Add the following operations to your program:
i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers.
ii. Delete a contact by having the user enter the name.
iii. Search the list by name and print the name and phone numbers for that one person.
iv. Print all contacts and phone numbers for each contact.
c. Create a program to test your class. The program should have a menu like this one.
1. Add a contact
2. Delete a contact
3. Print all contacts
4. Search for a contact
5. Exit
Here is how your node will look: (This is going to be a struct) The struct will contain a string, an array of 3 elements and the pointer to the next node.
Name
Phone Numbers 2145556678 8179992222 2143555555
Pointer to Next Node
Test: Take a screen shot or screen shots testing your program. Enter two contacts, print all contacts, search for a contact, delete a contact, print all contacts again so you can see the deleted contact is gone, search for the deleted contact.
Source Code: waitlist.cpp, waitlist.h and Lab8.cpp
Explanation / Answer
waitlist.h
#ifndef CONTACTLIST_H
#define CONTACTLIST_H
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node
{
string nam;
long int phNo[3];
struct Node *next;
};
class ContactList
{
Node *head;
public:
ContactList()
{
head=NULL;
}
void addContact();
void delContact();
void dispList();
void searchContact();
};
#endif
waitlist.cpp
#include "waitlist.h"
// Function to add a new contact
void ContactList :: addContact()
{
Node *nName=NULL;
nName=new Node;
cout<<"Enter Name : ";
cin>>nName->nam;
cout<<"Enter 3 Phone numbers : ";
for(int i=0; i<3; i++)
cin>>nName->phNo[i];
// if adding is first contact
if(head==NULL)
{
// new node is head
head=nName;
// head next is null
head->next=NULL;
// exit from function
return;
}
// for traversing
Node *tmp=head;
// traverse upto null before
while(tmp->next!=NULL)
tmp=tmp->next;
// add new node at end
tmp->next=nName;
// finally add null
nName->next=NULL;
}
// Function to Delete a contact
void ContactList :: delContact()
{
string nam;
// point head
Node *t1=head;
Node *t2=head;
cout<<"Enter name to delete contact : ";
// input for deletion name
cin>>nam;
// traverse upto name found
while(t1!=NULL)
{
// if found delete it
if(t1->nam==nam)
{
// skip the found contact
t2->next=t1->next;
// delete contact element
free(t1);
cout<<" Contact deleted..." <<endl;
return;
}
// set t2 with t1
t2=t1;
// go t1 next
t1=t1->next;
}
// if not found error message
if(t1==NULL)
{
cout<<" Sorry! no such contact..." <<endl;
// exit from function
return;
}
}
// Function to display all contacts
void ContactList :: dispList()
{
Node *tmp=head;
cout<<"List of contacts : ";
while(tmp!=NULL)
{
cout<<" Name: "<<tmp->nam<<endl;
cout<<"Phone nos : ";
for(int i=0; i<3; i++)
cout<<tmp->phNo[i]<<" ";
// goto next node
tmp=tmp->next;
cout<<endl;
}
}
// Function to Search the list by name
void ContactList :: searchContact()
{
string nam;
Node *t1=head;
cout<<"Enter name to search contact : ";
cin>>nam;
// traverse upto name found
while(t1!=NULL)
{
// if found the contact
if(t1->nam==nam)
{
cout<<"Name: "<<t1->nam<<endl;
cout<<"Phone nos : "<<endl;
for(int i=0; i<3; i++)
cout<<t1->phNo[i]<<" ";
break;
}
t1=t1->next;
}
if(t1==NULL)
{
cout<<" Sorry! no such contact..." <<endl;
// exit from function
return;
}
}
Lab8.cpp
#include "waitlist.h"
int main()
{
ContactList cl;
int ch=0;
while(ch!=5)
{
cout<<" 1.Add a contact "<<endl;
cout<<"2.Delete a contact "<<endl;
cout<<"3.Print all contacts "<<endl;
cout<<"4.Search a contact "<<endl;
cout<<"5.Exit "<<endl;
cout<<"Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
cl.addContact();
break;
case 2:
cl.delContact();
break;
case 3:
cl.dispList();
break;
case 4:
cl.searchContact();
break;
case 5:
cout<<" Thank you...";
exit(0);
break;
default:
cout<<" Invalid choice";
}
}
return 0;
}
------------------------------------------------------------------------------
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.