Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++, simple contactlist program Needs to be done using linked list Define a clas

ID: 666440 • Letter: C

Question

C++, simple contactlist program

Needs to be done using linked list

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.

Add the following operations to your program:

Add a new contact.  Ask the user to enter the name and up to 3 phone numbers.

Delete a contact by having the user enter the name.

Search the list by name and print the name and phone numbers for that one person.

Print all contacts and phone numbers for each contact.

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

Explanation / Answer

#include <iostream>
#include <malloc.h>
#include <string>
using namespace std;

class contactList{
public:
   string name;
   int phone[3];
   contactList *next;
   contactList(string n, int p[]){
       name = n;
       for(int i = 0; i < 3; i++){
           phone[i] = p[i];
       }
       next = NULL;
   }
};

int count = 0;
contactList *head = (contactList *)malloc(sizeof(contactList *));
void add(string n, int num[]){
   if(count == 0){
       head->name = n;
       for(int i = 0; i < 3; i++) head->phone[i] = num[i];
       head->next = NULL;
       count = 1;
       return;
   }
   contactList temp(n, num);
   contactList *newNode = &temp;
   contactList *cur = head;
   while(cur != NULL){
       if(cur->next == NULL){
           cur->next = newNode;
           return;
       }
       cur = cur->next;
   }
}
void delete1(string name){
   contactList *cur = head, *temp = head;
   while(cur != NULL){
       if(cur->name.compare(name) == 0){
           head->next = cur->next;
           free(cur);
           return;
       }
       temp = cur;
       cur = cur->next;
   }
   cout << "Name not found ";
}
void print(){
   contactList *cur = head;
   while(cur != NULL){
       cout << "Name: " << cur->name << " ";
       for(int i = 0; i < 3; i++){
           cout << "Number " << i + 1 << ": " << cur->phone[i] << " ";
       }
       cout << " ";
       cur = cur->next;
   }
}
bool search(string n){
   contactList *cur = head;
   while(cur != NULL){
       if(cur->name.compare(n) == 0) return true;
       cur = cur->next;
   }
   return false;
}


int main(){
   while(true){
       int choice;
       cout << "1. Add a contact 2. Delete a contact 3. Print all contacts 4. Search for a contact 5. Exit ";
       cout << "Enter your choice: ";
       cin >> choice;
       string name;
       int num[3];
       switch(choice){
       case 1:
           cout << "Enter the name to add: ";
           cin >> name;
           cout << "Enter 3 numbers to add: ";
           for(int i = 0; i < 3; i++){
               cin >> num[i];
           }
           add(name, num);
           break;
       case 2:
           cout << "Enter the name to delete: ";
           cin >> name;
           delete1(name);
           break;
       case 3:
           print();
           break;
       case 4:
           cout << "Enter the name to search: ";
           cin >> name;
           if(search(name) == true){
               cout << "Name found ";
           }
           else cout << "Name not found ";
           break;
       case 5:
           return 0;
       default:
           cout << "Invalid choice ";
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote