C++ program: Please use struct to create the linked list You will design an onli
ID: 3866260 • Letter: C
Question
C++ program: Please use struct to create the linked list
You will design an online contact list to keep track of names and plane numbers. a) Define a class contactlist that can store a name and upto 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 upto 3 phone numbers. ii) Delete a contact by having the user enter the name. iii) Search the list by name and print the name & phone numbers for that one person. 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 contactExplanation / Answer
Given below is the code with output. Please do rate the answer if it helped. Thank you.
contacts.h
#ifndef contacts_h
#define contacts_h
#include <iostream>
using namespace std;
typedef struct node
{
string contactName;
string phoneNumbers[3];
int numPhones;
struct node * next;
}contact;
//an alphabetically sorted list of contacts
class contactList
{
contact* head;
public:
contactList();
~contactList();
void addContact(string name, string *phones, int numPhones);
bool removeContact(string name);
void searchContact(string name);
void printAll();
};
#endif /* contacts_h */
contacts.cpp
#include "contacts.h"
#include <iomanip>
using namespace std;
contactList::contactList()
{
head = NULL;
}
contactList::~contactList()
{
contact *curr = head, * temp;
while(curr != NULL)
{
temp = curr->next;
delete curr;
curr = temp;
}
}
void contactList::addContact(string cname, string *phoneNos, int count)
{
contact *c = new contact();
c->contactName = cname;
for(int i = 0; i < count; i++)
c->phoneNumbers[i] = phoneNos[i];
c->numPhones = count;
c->next = NULL;
if(head == NULL)
head = c;
else
{
contact *curr = head, *prev = NULL;
//find the correct place to insert
while(curr != NULL)
{
if(cname > curr->contactName)
{
prev = curr;
curr = curr->next;
}
else
break;
}
//check if inserting before the 1st node
if(prev == NULL)
{
c->next = head;
head = c;
}
else
{
c->next = curr;
prev->next = c;
}
}
}
bool contactList::removeContact(string cname)
{
contact *curr = head, *prev = NULL;
//search for the contact, keeping track of the previous node
while(curr != NULL)
{
if(curr->contactName == cname)
break;
prev = curr;
curr = curr->next;
}
//check whether contact was found or nor
if(curr == NULL)
return false;
else
{
if(prev == NULL) //removing head node?
head = head->next;
else
prev->next =curr->next;
delete curr;
return true;
}
}
void contactList::searchContact(string cname)
{
contact *curr = head;
while(curr != NULL)
{
if(curr->contactName == cname)
break;
curr = curr->next;
}
if(curr == NULL)
cout << cname << " not found." << endl;
else
{
cout << "Found " << cname << " with " << curr->numPhones << " phones: " ;
for(int i = 0; i < curr->numPhones; i++)
cout << curr->phoneNumbers[i] << " ";
cout << endl;
}
cout << endl;
}
void contactList::printAll()
{
contact *curr = head;
cout << setw(15) << left << "Name" ;
cout << setw(15) << "Phone 1" ;
cout << setw(15) << "Phone 2" ;
cout << setw(15) << "Phone 3" << endl;
while(curr != NULL)
{
cout << setw(15) << left << curr->contactName;
for(int i = 0; i < curr->numPhones; i++)
cout << setw(15) << curr->phoneNumbers[i];
curr = curr->next;
cout << endl;
}
cout << endl;
}
contactsmain.cpp
#include <iostream>
#include "contacts.h"
using namespace std;
int main()
{
int count, choice = 1;
contactList list;
string phones[3], name;
while(choice != 5)
{
cout << "1. Add a contact" << endl;
cout << "2. Delete a contact" << endl;
cout << "3. Print all" << endl;
cout << "4. Search a contact" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "Adding a contact..." << endl;
cout << "Enter name: ";
cin >> name;
count = 0;
while(count < 1 || count > 3)
{
cout << "Enter number of phone numbers[1-3]: " ;
cin >> count;
}
for(int i = 0; i < count; i++)
{
cout << "Enter Phone Number " << (i+1) << ": ";
cin >> phones[i];
}
list.addContact(name, phones, count);
cout << "Contact added." << endl << endl;
break;
case 2:
cout << "Deleting a contact..." << endl;
cout <<"Enter name of contact: ";
cin >> name;
if(list.removeContact(name))
cout << "Deleted contact " << name << endl << endl;
else
cout << "Not found contact " << name << endl << endl;
break;
case 3:
list.printAll();
break;
case 4:
cout << "Searching for a contact..." << endl;
cout <<"Enter name: ";
cin >> name;
list.searchContact(name);
break;
case 5:
break;
default:
cout << "Invalid menu choice. " << endl;
}
}
cout << "Program exiting." << endl;
return 0;
}
output
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 1
Adding a contact...
Enter name: John
Enter number of phone numbers[1-3]: 5
Enter number of phone numbers[1-3]: 2
Enter Phone Number 1: 48384333
Enter Phone Number 2: 92872229
Contact added.
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 1
Adding a contact...
Enter name: Henry
Enter number of phone numbers[1-3]: 1
Enter Phone Number 1: 999999999
Contact added.
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 3
Name Phone 1 Phone 2 Phone 3
Henry 999999999
John 48384333 92872229
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 4
Searching for a contact...
Enter name: Bob
Bob not found.
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 4
Searching for a contact...
Enter name: Henry
Found Henry with 1 phones: 999999999
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 2
Deleting a contact...
Enter name of contact: John
Deleted contact John
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 3
Name Phone 1 Phone 2 Phone 3
Henry 999999999
1. Add a contact
2. Delete a contact
3. Print all
4. Search a contact
5. Exit
Enter your choice: 5
Program exiting.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.