Write a C++ address book program that will accomplish the following: 1. Read nam
ID: 3700103 • Letter: W
Question
Write a C++ address book program that will accomplish the following:
1. Read name and address data from the user either from the keyboard or from a file.
2. While reading the names and addresses, put the names into an appropriate data structure.
3. After reading names and addresses, allow user to search for names and change the names or addresses in the container data structure.
4. Allow user to write out the container data structure to a comma delimited file, this file is different from the input file.
The input file will have the following form, 1 line for the name the next line for the address:
name
address
name
address
.
.
.
Do not use STL Data Structures.
Explanation / Answer
Answer :
#include<iostream>
using namespace std;
/*
* Node Declaration
*/
struct node {
string name;
string address;
struct node *next;
} *start;
/*
* Class Declaration
*/
class LinkedList {
public:
node *create_node(string, string);
void insert_begin();
void delete_pos();
void search();
void update();
void display();
LinkedList() {
start = NULL;
}
};
/*
* Main :contains menu
*/
int main() {
int choice, nodes, element, position, i;
LinkedList sl;
start = NULL;
while (1) {
cout << endl << "---------------------------------" << endl;
cout << endl << " PHONE BOOK " << endl;
cout << endl << "---------------------------------" << endl;
cout << "1.Add Contact" << endl;
cout << "2.Delete Contact" << endl;
cout << "3.Update Contact " << endl;
cout << "4.Search Contact" << endl;
cout << "5.Display Contact" << endl;
cout << "6.Exit " << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice) {
case 1:
cout << "Adding a Contact: " << endl;
sl.insert_begin();
cout << endl;
break;
case 2:
cout << "Delete a Contact: " << endl;
sl.delete_pos();
cout << endl;
break;
case 3:
cout << "Update Node Value:" << endl;
sl.update();
cout << endl;
break;
case 4:
cout << "Search Contact: " << endl;
sl.search();
cout << endl;
break;
case 5:
cout << "Display All Contact" << endl;
sl.display();
break;
case 6:
cout << "Exiting..." << endl;
exit(1);
default:
cout << "Wrong choice" << endl;
}
}
}
/*
* Creating Node
*/
node *LinkedList::create_node(string name, string address) {
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL) {
cout << "Memory not allocated " << endl;
return 0;
} else {
temp->name = name;
temp->address = address;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void LinkedList::insert_begin() {
string name;
string address;
cin.ignore(1, ' ');
cout << "Enter the name: ";
getline(cin, name);
cout << "Enter the Address: ";
getline(cin, address);
struct node *temp, *p;
temp = create_node(name, address);
if (start == NULL) {
start = temp;
start->next = NULL;
} else {
p = start;
start = temp;
start->next = p;
}
cout << "Contact added successfully" << endl;
}
/*
* Delete element at a given position
*/
void LinkedList::delete_pos() {
cin.ignore(1, ' ');
int pos, i, counter = 0;
string name;
if (start == NULL) {
cout << "Contact list is empty" << endl;
return;
}
cout << "Enter the name to be deleted: ";
getline(cin, name);
struct node *s, *ptr;
s = start;
if (s->name == name) {
start = s->next;
} else {
while (s != NULL) {
if (s->name == name) {
break;
}
ptr = s;
s = s->next;
}
ptr->next = s;
free(s);
cout << "Contact Deleted" << endl;
}
}
/*
* Update a given Node
*/
void LinkedList::update() {
cin.ignore(1, ' ');
string name;
string address;
string newName;
int pos, i;
if (start == NULL) {
cout << "List is empty" << endl;
return;
}
cout << "Enter the contact name be updated: ";
getline(cin, name);
cout << "Enter the New name: ";
getline(cin, newName);
cout << "Enter the Address: ";
getline(cin, address);
struct node *s, *ptr;
s = start;
if (s->name == name) {
if (!name.empty()) {
s->name = newName;
}
if (!address.empty()) {
s->address = address;
}
cout << "Contact Updated Successfully" << endl;
}
else {
while (s != NULL) {
if (s->name == name) {
if (!name.empty()) {
s->name = newName;
}
if (!address.empty()) {
s->address = address;
}
cout << "Contact Updated Successfully" << endl;
break;
}
s = s->next;
}
}
}
/*
* Searching an element
*/
void LinkedList::search() {
cin.ignore(1, ' ');
string name;
int pos = 0;
bool flag = false;
if (start == NULL) {
cout << "List is empty" << endl;
return;
}
cout << "Enter the Name to be searched: ";
getline(cin, name);
struct node *s;
s = start;
while (s != NULL) {
pos++;
if (s->name == name) {
flag = true;
cout << "Name: " << name << " is found at position " << pos << endl;
cout << "Address:" << s->address << endl;
}
s = s->next;
}
if (!flag)
cout << "Element " << name << " not found in the list" << endl;
}
/*
* Display Elements of a link list
*/
void LinkedList::display() {
struct node *temp;
if (start == NULL) {
cout << "The List is Empty" << endl;
return;
}
temp = start;
cout << "Elements of list are: " << endl;
while (temp != NULL) {
cout << temp->name << ",";
cout << temp->address << endl;
temp = temp->next;
}
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.