Question

a. Define the struct node that can store a name and up to 3 phone numbers...

a. Define the struct node that can store a name and up to 3 phone numbers (use an array for the phone numbers). The node struct will also store the address to the next node.

b. Define a class contactList that will define a variable to keep track of the number of contacts in the list, a pointer to the first and last node of the list.

c. Add the following methods to the class:

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.

d. 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

Language: C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution if you have any doubts then you can write in the comment section.

Please give feedback.

Solution:


#include <bits/stdc++.h>

using namespace std;
//struct node
typedef struct node
{
    //variables
    string name;
    string phone[3];
    struct node *next;
}Node;
//class contactList
class contactList
{
    //variables
    int numOfContacts;
    struct node *first;
    struct node *last;
    public:
    //constructor
    contactList()
    {
        numOfContacts=0;
        first=NULL;
        last=NULL;
    }
    //function AddContact() to add contact in list
    void AddContact()
    {
        //ignore newline character otherwise name will store newline character 
        cin.ignore();
        int n;
        //create a new node
        Node *temp=new Node;
        //enter data from user
        cout<<"Enter Name: ";
        getline(cin,temp->name);
        cout<<"How many phone number do you have? :";
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cout<<"Enter Phone number "<<i+1<<" :";
            cin>>temp->phone[i];
        }
        temp->next=NULL;
        //if list is Empty then add contact at first
        if(first==NULL)
        {
            first=temp;
            //set last = first
            last=temp;
            first->next=NULL;
            last->next=NULL;
            
        }
        //if list is not Empty
        else
        {
            //define pointer which points first
            Node *itr=first;
            //iterate until itr reach at last node
            while(itr!=last)
            itr=itr->next;
            //add temp as last node
            itr->next=temp;
            //update last Node
            last=temp;
            
        }
        //increment in numOfContacts
        numOfContacts++;
    }
    //function to Delete Contact
    void DeleteContact(string name)
    {
        Node *itr=first;
        //if list is Empty then Print message and return
        if(itr==NULL)
        {
                cout<<"List is Empty!\n";
                return;
                }
                //if Contact is to be Deleted is first node
        if(itr->name==name)
        {
            //temp will point node to be deleted
            Node *temp=itr;
            //set first = first->next
            first=first->next;
            //delete temp
            delete temp;
            cout<<"Contact Deleted Successfully!\n";
        }
        //otherwise iterate through list
        else
        {while(itr!=last)
        {
            //if Contact found then break
            if(itr->next->name==name)
            break;
            itr=itr->next;
                
        }
        //if Contact found
        if(itr!=last)
        {
            //Print message
            cout<<"Contact Deleted Successfully!\n";
            Node *temp=itr->next;
            itr->next=itr->next->next;
            delete temp;
            //if node which was deleted was last node then update last node
            if(itr->next==NULL)
                last=itr;
            //decrement numOfContacts
            numOfContacts--;
            
        }
        //if Contact not Found
        else
        {
            cout<<"Contact Not Found!\n";
        }
        }
        
        
    }
    
    //function to Search Contact
    void SearchContact(string name)
    {
        
        Node *itr=first;
        //if list is Empty then Print message and return
        if(itr==NULL)
        {
                cout<<"List is Empty!\n";
                return;
                }
                //iterate until name not Found
        while(itr->name!=name)
        {
            itr=itr->next;
            if(itr==NULL)
            break;
                
        }
        //if name Found
        if(itr!=NULL)
        {
            //Print node
            cout<<"Name: "<<itr->name<<"\n";
            
            for(int i=0;i<3;i++)
            {
                if(itr->phone[i]!="")
                cout<<"Phone Number "<<i+1<<": "<<itr->phone[i]<<"\n";
            }
            
        }
        //if name not found
        else
        {
            cout<<"Contact is not in the list!\n";
        }
    }
    
    //print All Contacts
    void printAllContacts()
    {
        Node *itr=first;
        if(itr==NULL)
        {
            cout<<"List is Empty!\n";
            return;
        }
        //iterate through list and print All Contacts
        while(itr!=NULL)
        {
            cout<<"Name: "<<itr->name<<"\n";
            
            for(int i=0;i<3;i++)
            {
                if(itr->phone[i]!="")
                cout<<"Phone Number "<<i+1<<": "<<itr->phone[i]<<"\n";
            }
            cout<<"\n\n\n";
            itr=itr->next;
        }
    }
};
int main()
{
    //create object of contactList
    contactList clist;
    int choice;
    string name;
    //loop until user want
    while(true)
    {
        //Print choices
        cout<<"1. Add a contact\n";
        cout<<"2. Delete a contact\n";
        cout<<"3. Print all contacts\n";
        cout<<"4. Search for a contact\n";
        cout<<"5. Exit\n";
        cin>>choice;
        //switch case for different choices
        switch(choice)
        {
            case 1:{
                        clist.AddContact();
                        break;
                   }
            case 2:{    
                        cin.ignore();
                        cout<<"Enter Name: ";
                        getline(cin,name);
                        clist.DeleteContact(name);
                        break;
                   }
            case 3:{
                        clist.printAllContacts();
                        break;
                   }
            case 4:{    
                        cin.ignore();
                        cout<<"Enter Name: ";
                        getline(cin,name);
                        clist.SearchContact(name);
                        break;
                   }
            case 5:{
                        cout<<"Thank You!";
                        exit(0);
                   }
            default:{
                        cout<<"Enter Valid choice!\n";
                        break;
                    }
            
        }
    }

    return 0;
}

Output:

5. Exit I C:\Users\Rampratap Meel\Desktop\rpmeel.exe 1. Add a contact 2. Delete a contact 3. Print all contacts 4. Search forХ 1. Add I C:\Users\Rampratap Meel\Desktop\rpmeel.exe Name: Ajay Phone Number 1: 987654321 Phone Number 2: 654654654 а contacC:\Users\Rampratap Meel\Desktop\rpmeel.exe Contact Not Found! 1. Add a contact 2. Delete a contact 3. Print all contacts 4. S

Thank You!

Add a comment
Know the answer?
Add Answer to:
a. Define the struct node that can store a name and up to 3 phone numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) 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...

  • This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone...

    This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone book using a linked list to keep people's names and phone numbers organized in alphabetical order. 1. Define a structure to hold the contact information including: name, phone number, a pointer to the next node in the list. Example: struct ContactNode { string name; string phoneNumber; ContactNode *next; } 2. Define a class containing the structure and the appropriate methods to update and retrieve...

  • Write in C++ please: Write a class named MyVector using the following UML diagram and class...

    Write in C++ please: Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked listed instead of an array. Each Contact is a struct that will store a name and a phone number. Demonstrate your class in a program for storing Contacts. The program should present the user with a menu for adding, removing, and retrieving Contact info. MyVector Contact name: string phone: string next: Contact -head: Contact -tail: Contact -list_size:...

  • Need help working through CLion: Description: For this assignment you will create a program to manage...

    Need help working through CLion: Description: For this assignment you will create a program to manage phone contacts. Your program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. Your program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2....

  • write a ContactBook in C++ ContactBook that holds 10 names with that names corresponding contact (last...

    write a ContactBook in C++ ContactBook that holds 10 names with that names corresponding contact (last name and first name of the owner). Ask the user to input up to 10 Contacts (It may be less. The user should have the ability to stop inputting Contacts whenever he wishes). 3.This class will store a list of Contacts in a stack allocated array of default capacity 10. 4.You must be able to add new contacts to a list, delete old contacts,...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • C++: Array of contact info Design a contact struct, that takes your phone struct and address...

    C++: Array of contact info Design a contact struct, that takes your phone struct and address struct along with a c string for a name to create a record of contact information. ( C++: Design a Struct Design two structs address and phone Address has the following attributes: street address, city name, state code, zip code. phone has 3 numbers: area code, prefix and suffix test these by writing a program that creates one of each and fills them with...

  • language:python VELYIEW Design a program that you can use to keep information on your family or...

    language:python VELYIEW Design a program that you can use to keep information on your family or friends. You may view the video demonstration of the program located in this module to see how the program should function. Instructions Your program should have the following classes: Base Class - Contact (First Name, Last Name, and Phone Number) Sub Class - Family (First Name, Last Name, Phone Number, and Relationship le. cousin, uncle, aunt, etc.) Sub Class - Friends (First Name, Last...

  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT