Question

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 {
        return email;
    }

    void setEmail(string email) {
        this->email = email;
    }

    string getPhone() const {
        return phone;
    }

    void setPhone(string phone) {
        this->phone = phone;
    }
};

int main() {
    Contact contacts[10];
    contacts[0].setName("Chris");
    contacts[0].setEmail("Chris's mail");
    contacts[0].setPhone("Chris's number");

    contacts[1].setName("Maciah");
    contacts[1].setEmail("Maciah's mail");
    contacts[1].setPhone("Maciah's number");

    contacts[2].setName("Brigg");
    contacts[2].setEmail("Brigg's mail");
    contacts[2].setPhone("Brigg's number");

    // change contact of Chris
    contacts[0].setPhone("Chris's second number");

    for (int i = 0; i < 3; ++i) {
        cout << "Name: " << contacts[i].getName() << ", Mail: " << contacts[i].getEmail() << ", Number: " << contacts[i].getPhone() << endl;
    }
    return 0;
}

CAN YOU HELP WITH THE FOLLOWING PROMPT:

Use the Contact class from ABOVE to create a ContactLinkedList class with all your contacts sorted in alphabetical order by name. The ContactLinkedList will have as functions:

  • insert a contact takes a Contact object as parameters and inserts in the appropriate position,
  • delete a contact takes a name as parameter and deletes the Contact with that name,
  • display all contacts.

Each Contact has stored as data a name, email, and phone. There should be a Contact * to the next Contact in the linked list. The Contact * can either be public in the Contact class, OR you could use a struct ListNode as shown in the slides (see below).

Take a look how it was implemented in the slides:

Contents of ContactLinkedList.h

1 // Specification file for the ContactLinkedList class
2 #ifndef CONTACTLINKEDLIST_H
3 #define CONTACTLINKEDLIST_H
4
5 class ContactLinkedList
6 {
7  
8     // Declare a structure for the list
9     struct ContactNode
10     {
11        Contact value;           // The value in this node (a Contact object)
12        struct ContactNode *next; // To point to the next node
13     };
14
15 ContactNode *head;            // List head pointer
16 .....

void insert(ContactNode);

void delete(string name);

void display();

}

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

//contact.h

#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 {
return email;
}

void setEmail(string email) {
this->email = email;
}

string getPhone() const {
return phone;
}

void setPhone(string phone) {
this->phone = phone;
}
};

//contactlinkedlist.h

#include"contact.h"
#ifndef CONTACTLINKEDLIST_H
#define CONTACTLINKEDLIST_H

class ContactLinkedList
{
private:
// Declare a structure for the list
struct ContactNode
{
Contact value; // The value in this node (a Contact object)
struct ContactNode *next; // To point to the next node
};
ContactNode *head; // List head pointer

public:
    ContactLinkedList(){
        head=NULL;
}

void insert(Contact);
void Delete(string name);
void display();

};
#endif

//contactlinkedlist.cpp

#include"contactlinkedlist.h"

void ContactLinkedList::insert(Contact c){
   ContactNode *newnode = new ContactNode;
   newnode->value = c;
   newnode->next=NULL;
  
   if(!head ||head->value.getName()>c.getName()){
   newnode->next = head;
   head=newnode;
   return;
           }
       ContactNode * previous = head;
       ContactNode * current = head->next;
       while (current != NULL && c.getName() > current->value.getName())
       {
       previous = current;
       current = current->next;
       }
   previous->next = newnode;
        newnode->next = current;
}

void ContactLinkedList::Delete(string data){
          
       ContactNode* current = head,*prev;
           if(current->value.getName() ==data){
               head = current->next;
               delete current;
               return;
           }
           while(current && current->value.getName()!=data){
               prev = current;
               current = current->next;
           }
           if(!current){
               cout<<data<<" is not in list\n";
               return;
           }
           prev->next = current->next;
           delete current;
   }
  
void ContactLinkedList::display(){
       ContactNode * current = head;
           while ( current ) {
               cout<<"Name : "<<current->value.getName()<<"\n";
               cout<<"Email : "<<current->value.getEmail()<<"\n";
               cout<<"Phone : "<<current->value.getPhone()<<"\n";
               current = current->next;
               cout<<"\n\n";
           }
          
}

//main.cpp

#include"contactlinkedlist.cpp"

int main() {
Contact contacts[10];
contacts[0].setName("Chris");
contacts[0].setEmail("Chris's mail");
contacts[0].setPhone("Chris's number");

contacts[1].setName("Maciah");
contacts[1].setEmail("Maciah's mail");
contacts[1].setPhone("Maciah's number");

contacts[2].setName("Brigg");
contacts[2].setEmail("Brigg's mail");
contacts[2].setPhone("Brigg's number");

ContactLinkedList list;

for (int i = 0; i < 3; ++i) {
list.insert(contacts[i]);
}
cout<<"Your current list is \n";
list.display();
return 0;
}

//sample output

      

Add a comment
Know the answer?
Add Answer to:
USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...
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
  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12...

    // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12 #define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'" namespace Errors {     struct InvalidName { }; } class Name { public:     Name        ( string first_name = "", string last_name = "");     string first() const;     string last() const;     void   set_first( string fname );     void   set_last( string lname);     friend ostream& operator<< ( ostream & os, Name name );     friend bool operator== (Name name1, Name...

  • I'm struggling to figure out how to do this problem in Java, any help would be...

    I'm struggling to figure out how to do this problem in Java, any help would be appreciated: Create an application that uses a class to store and display contact information. Console: Welcome to the Contact List application Enter first name: Mike Enter last name: Murach Enter email:      mike@murach.com Enter phone:      800-221-5528 -------------------------------------------- ---- Current Contact ----------------------- -------------------------------------------- Name:           Mike Murach Email Address: mike@murach.com Phone Number:   800-221-5528 -------------------------------------------- Continue? (y/n): n Specifications Use a class named Contact to store the data...

  • Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode ha...

    Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • A class called Author is designed as shown in the class diagram. (JAVA)

    in javaA class called Author is designed as shown in the class diagram. Author name: String email: String gender: char +Author (name :String, email: String, gender char) +getName ():String +getEmail() String +setEmail (email: String) :void +getGender(): char +toString ():String It contains  Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f),  One constructor to initialize the name, email and gender with the given values; public Author (String name, String email, char gender) f......)  public getters/setters: getName(), getEmail setEmail and getGender(); (There are no setters for name and gender,...

  • I need to create file called EvilPerson.java. This implements a class that inherits from Person. The...

    I need to create file called EvilPerson.java. This implements a class that inherits from Person. The EvilPerson class doesn’t want you to be able to set a phone number for a user, so it always stores the text “n/a” in the phone attribute. You should define a constructor for EvilPerson that takes in a name and a phone number, stores the name correctly but throws away the phone number values and stores “n/a” there instead. The setPhone() method from Person...

  • Java need help with searching for a contact Question:Write a program that uses an ArrayList of...

    Java need help with searching for a contact Question:Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact’s first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

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