Design and implement a C++ program to maintain a list of contacts. Requirements:
ID: 658054 • Letter: D
Question
Design and implement a C++ program to maintain a list of contacts.
Requirements:
1. Contacts have a name, a phone number, and an email address. A contact is uniquely identified by name.
2. Have options for the user to:
a. Enter a contact, by entering the name, phone number, and email address. If the name is already in the list, display an error message.
b. Remove a contact, by entering the name. If the name is not in the list, display an error message.
c. Find a contact, by entering the name. If found, display the contact phone number and email address, otherwise display an error message.
d. List all contacts (name, phone number, and email address), in order by name.
3. Use console input-output.
4. Create a class to hold the contact list, with interface methods for each of the above options.
5. Use a binary search tree.
Explanation / Answer
Complete Program:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Contact
{
public:
Contact();
void setName(string);
void setNumber(int);
void setEmial(string);
string getName();
void printContact();
private:
string name;
int number;
string email;
};
Contact::Contact()
{
name = "";
number = 0;
email = "";
}
void Contact::setName(string cname)
{
name = cname;
}
void Contact::setNumber(int cnumber)
{
number = cnumber;
}
void Contact::setEmial(string cemail)
{
email = cemail;
}
string Contact::getName()
{
return name;
}
void Contact::printContact()
{
cout << name << ", " << number << ", " << email << endl;
}
struct node
{
Contact info;
struct node *left;
struct node *right;
}*root;
class BST
{
public:
BST();
void add(node *, node *);
void remove(string);
void find(string, node **, node **);
void print(node *);
private:
void removeHelper1(node *,node *);
void removeHelper2(node *,node *);
void removeHelper3(node *,node *);
};
BST::BST()
{
root = NULL;
}
void BST::add(node *tree, node *newnode)
{
if(root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
return;
}
if (strcmp(tree->info.getName().c_str(), newnode->info.getName().c_str()) == 0)
{
cout << newnode->info.getName() << " is already entered in the tree." << endl;
return;
}
if(strcmp(tree->info.getName().c_str(), newnode->info.getName().c_str()) > 0)
{
if (tree->left != NULL)
{
add(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
return;
}
}
else
{
if (tree->right != NULL)
{
add(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
return;
}
}
}
void BST::remove(string item)
{
if(root == NULL)
{
cout << "Tree is empty!" << endl;
return;
}
node *par, *pos;
find(item, &par, &pos);
if(pos == NULL)
{
cout << item << " is not found in the tree. " << endl;
return;
}
if (pos->left == NULL && pos->right == NULL)
removeHelper1(par, pos);
if (pos->left != NULL && pos->right == NULL)
removeHelper2(par, pos);
if (pos->left == NULL && pos->right != NULL)
removeHelper2(par, pos);
if (pos->left != NULL && pos->right != NULL)
removeHelper3(par, pos);
free(pos);
}
void BST::removeHelper1(node *par, node *pos)
{
if(par == NULL)
root = NULL;
else if(pos == par->left)
par->left = NULL;
else
par->right = NULL;
}
void BST::removeHelper2(node *par, node *pos)
{
node *current;
if(pos->left != NULL)
current = pos->left;
else
current = pos->right;
if(par == NULL)
root = current;
else if (pos == par->left)
par->left = current;
else
par->right = current;
}
void BST::removeHelper3(node *par, node *pos)
{
node *current, *save, *curr, *prev;
save = pos;
current = pos->right;
while(current->left != NULL)
{
save = current;
current = current->left;
}
curr = current;
prev = save;
if(curr->left == NULL && curr->right == NULL)
removeHelper1(prev, curr);
else
removeHelper2(prev, curr);
if(par == NULL)
root = curr;
else if(pos == par->left)
par->left = curr;
else
par->right = curr;
curr->left = pos->left;
curr->right = pos->right;
}
void BST::find(string item, node **par, node **loc)
{
node *current, *save;
if(root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if(strcmp(item.c_str(), root->info.getName().c_str()) == 0)
{
*loc = root;
*par = NULL;
return;
}
if(strcmp(item.c_str(), root->info.getName().c_str()) < 0)
current = root->left;
else
current = root->right;
save = root;
while(current != NULL)
{
if(strcmp(item.c_str(), current->info.getName().c_str()) == 0)
{
*loc = current;
*par = save;
return;
}
save = current;
if(strcmp(item.c_str(), current->info.getName().c_str()) < 0)
current = current->left;
else
current = current->right;
}
*loc = NULL;
*par = save;
}
void BST::print(node *current)
{
if (root == NULL)
{
cout << "Tree is empty!" << endl;
return;
}
if(current != NULL)
{
print(current->left);
current->info.printContact();
print(current->right);
}
}
int main()
{
string cname, cemail;
int cnumber, choice;
BST bst;
node *temp;
Contact cont;
do
{
cout << " ----MENU----" << endl;
cout << "1. Add contact " << endl;
cout << "2. Remove contact "<< endl;
cout << "3. Find contact" << endl;
cout << "4. Print contacts" <<endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
temp = new node;
cout << " Enter the name of the contact: ";
cin >> cname;
cout << "Enter the phone number: ";
cin >> cnumber;
cout << "Enter the emial id: ";
cin >> cemail;
cont.setName(cname);
cont.setNumber(cnumber);
cont.setEmial(cemail);
temp->info = cont;
bst.add(root, temp);
break;
case 2:
if(root == NULL)
cout << "Tree is empty!" << endl;
else
{
cout << " Enter the name of the contact: ";
cin >> cname;
bst.remove(cname);
}
break;
case 3:
cout << " Enter the name of the contact: ";
cin >> cname;
node *par, *pos;
bst.find(cname, &par, &pos);
if(pos == NULL)
cout << cname << " is not found in the tree." << endl;
else
{
cout << cname << " is found in the tree." << endl;
pos->info.printContact();
}
break;
case 4:
cout << "" << endl;
bst.print(root);
break;
case 0:
cout << "Thank you" << endl;
default:
cout << "Invalid choice!" << endl;
}
}while(choice != 0);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.