Question

Write in C++ please:

Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked lisClass Attributes: Contact: a private struct that contains the following three fields: o name: a string variable for storing aA 20-Point Sample Run: Run 1: (A)dd Contact |(C)lear Contacts (F)ind Contact (Q)uit |(R) emove Contact [ACFQR] a Enter name:Run 2: |(A)dd Contact |(C)lear Contacts |(F)ind Contact |(Q)uit |(R)emove Contact [ACFQR] f Enter name: Paul 555-775-1234 |(ARun 3: |(A)dd Contact |(C)lear Contacts (F)ind Contact |(e)uit (R) emove Cont act [ACFQR] c |(A)dd Contact |(C)lear ContactsRun 4: [ACFQR] a Enter name: Tom Enter phone : 510-555-5309 |(A)dd Contact (C)lear Contacts (F)ind Contact |(Q)uit (R)emove C

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: int +MyVector(): |MyVector() +push_back(name: string, phone: string): void +remove(name: string, phone: string): void +get(name: string): string +clear): void +size():int Class Attributes: Contact: a private struct that contains the following three fields: o name: a string variable for storing a Contact's name. o phone: a string variable for storing a Contact's phone number. o next: a self referencing pointer. head: a private Contact node pointer for storing the memory address of the first Contact node in the list. tail: a private Contact node pointer for storing the memory address of the last Contact node in the list. list_size: a private int variable that stores the number of Contact nodes in the list. MyVector: The constructor. It sets list_size to 0 and head and tail to null. MyVector: The destructor. This frees the memory all memory used by the list. push_back: This public method accepts the name and phone number of a contact as it's only arguments. If there is enough room, it adds the Contact to the end of the list of Contacts. Updates list_size as needed. Executes in constant (O(1)) time.
Class Attributes: Contact: a private struct that contains the following three fields: o name: a string variable for storing a Contact's name. o phone: a string variable for storing a Contact's phone number. o next: a self referencing pointer head: a private Contact node pointer for storing the memory address of the first Contact node in the list. tail: a private Contact node pointer for storing the memory address of the last Contact node in the list. list_size: a private int variable that stores the number of Contact nodes in the list. MyVector: The constructor. It sets list_size to 0 and head and tail to null MyVector: The destructor. This frees the memory all memory used by the list push_back: This public method accepts the name and phone number of a contact as it's only arguments. If there is enough room, it adds the Contact to the end of the list of Contacts. Updates list_size as needed. Executes in constant (O(1)) time. remove: This public method accepts two strings as it's only arguments. This method then searches the Contacts list for a matching name or phone number or both. If found, the matching Contact is "removed" from the list and updates the list_size variable get: This public method accepts the name of a Contact as it's only argument. It then searches the Contacts list for a match. If it finds one, it returns the phone number for that contact as a string, otherwise, it returns the string "NOT FOUND". clear: This public method clears the Contacts list by freeing all memory used by the list and resetting head and tail to null. size: returns the number of Contacts in the list as an integer. No method interacts with the user so no cin or cout statements. The main) function is responsible for interacting with the user Your submission must compile and run. If it does not, it will get 0 points.
A 20-Point Sample Run: Run 1: (A)dd Contact |(C)lear Contacts (F)ind Contact (Q)uit |(R) emove Contact [ACFQR] a Enter name: Paul Enter phone: 555-775-1234 |(A)dd Contact |(C)lear Contacts (F)ind Contact (Q)uit (R)emove Contact [ACFQR]:
Run 2: |(A)dd Contact |(C)lear Contacts |(F)ind Contact |(Q)uit |(R)emove Contact [ACFQR] f Enter name: Paul 555-775-1234 |(A)dd Contact |(C)lear Contacts |(F)ind Contact |(Q) uit |(R)emove Contact [ACFQR]:
Run 3: |(A)dd Contact |(C)lear Contacts (F)ind Contact |(e)uit (R) emove Cont act [ACFQR] c |(A)dd Contact |(C)lear Contacts (F)ind Contact |(e)uit |(R)emove Contact [ACFQR]: f Enter name: Paul NOT FOUND |(A)dd Contact |(C)lear Contacts |(F)ind Contact |(0)uit (R) emove Contact [ACFQR]:
Run 4: [ACFQR] a Enter name: Tom Enter phone : 510-555-5309 |(A)dd Contact (C)lear Contacts (F)ind Contact |(Q)uit (R)emove Contact [ACFQR] r Enter the nameor |(A)dd Contact (C)lear Contacts |(F)ind Contact (Q)uit (R)emove Contact number of a contact to remove : 510-555-5309 [ACFQR]: f Enter name: Tom NOT FOUND |(A)dd Contact |(C)lear Contacts (F)ind Contact (Q)uit (R)emoveContact [ACFQR]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Class Interface

#ifndef MyVector_Included
#define MyVector_Included

#include <string>
using namespace std;

struct Contact{
   string name;
   string phone;
   Contact* next;
};

class MyVector {
public:
   MyVector();
   ~MyVector();
   void push_back(string name1, string phone1);
   void remove(string name1, string phone1);
   string get(string name1);
   void clear();
   int size();
  
private:
   Contact* head;
   Contact* tail;
   int list_size;
};

#endif

Class Implementation

#include "MyVector.h"

MyVector::MyVector() {
   list_size = 0;
   head = NULL;
   tail = NULL;
}

MyVector::~MyVector() {
   clear();
}

void MyVector::push_back(string name1, string phone1) {
   if (list_size == 0) {
       head = new Contact;
       tail = new Contact;
   }
   Contact* newTail = new Contact;
   newTail->name = name1;
   newTail->phone = phone1;
   tail->next = newTail;
   tail = newTail;
   if (list_size == 0)
       head = tail;
   list_size++;
}

void MyVector::remove(string name1, string phone1) {
   if (list_size == 0) {
       return;
   }
   if (list_size == 1) {
       if (head->name == name1 || head->phone == phone1) {
           delete head;
           head = NULL;
           tail = NULL;
           list_size--;
           return;
       }
   }
   list_size--;
   if (head->name == name1 || head->phone == phone1) {
       Contact* toDelete = head;
       head = head->next;
       delete toDelete;
       return;
   }
   Contact* head1 = head;
   Contact* previous = NULL;
   while (head1->next != NULL) {
       if (head1->next->name == name1 || head1->next->phone == phone1) {
           Contact* toDelete = head1->next;
           head1->next = head1->next->next;
           delete toDelete;
           return;
       }
       previous = head1;
       head1 = head1->next;
   }
   if (head1->next->name == name1 || head1->next->phone == phone1) {
       delete head1;
       tail = previous;
   }
}

string MyVector::get(string name1) {
   Contact* head1 = head;
   while (head1 != NULL) {
       if (head1->name == name1) {
           return head1->phone;
       }
       head1 = head1->next;
   }
   return "NOT_FOUND";
}

void MyVector::clear() {
   if (list_size == 0)
       return;
   while (head != NULL) {
       Contact* current = head;
       head = head->next;
       delete current;
   }
   list_size = 0;
   head = NULL;
   tail = NULL;
}

int MyVector::size() {
   return list_size;
}

Main function

#include <iostream>
#include "MyVector.h"
using namespace std;

int main () {
  
   MyVector vector = MyVector();
   while (true) {
       cout << "(A)dd Contacts" << endl;
       cout << "(C)lear Contacts" << endl;
       cout << "(F)ind Contact" << endl;
       cout << "(Q)uit" << endl;
       cout << "(R)emove Contact" << endl;
       cout << endl;
       cout << "[ACFQR]: ";
       char choice;
       cin >> choice;
       cout << endl;
       if (choice == 'a') {
           string name, phone;
           cout << "Enter name: ";
           cin >> name;
           cout << endl;
           cout << "Enter phone: ";
           cin >> phone;
           cout << endl;
           vector.push_back(name, phone);
       } else if (choice == 'c') {
           cout << endl;
           vector.clear();
       } else if (choice == 'f') {
           string name;
           cout << "Enter name: ";
           cin >> name;
           cout << vector.get(name) << endl;
       } else if (choice == 'q') {
           break;
       } else if (choice == 'r') {
           string nameOrPhone;
           cout << "Enter the name or number of a contact to remove: ";
           cin >> nameOrPhone;
           vector.remove(nameOrPhone, nameOrPhone);
       }
   }

   return 0;
}

|(A)dd Contacts |(C)lear Contacts |(F) ind Contact |(Q)uit (R) emove Contact [ACFQR]: a Enter name: Paul Enter phone: 555-775

Enter the name or number of a contact to remove: Paul (A)dd Contacts (C)lear Contacts (F)ind Contact (Q)uit (R)emove Contact

comment down for any queries

please give a thumbs up

Add a comment
Know the answer?
Add Answer to:
Write in C++ please: Write a class named MyVector using the following UML diagram and class...
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
  • 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 {...

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

  • 20.6 Lab: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes (1) Create three files to submit. ContactNode.h-Class declaration ContactNode.cpp- Class...

    20.6 Lab: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes (1) Create three files to submit. ContactNode.h-Class declaration ContactNode.cpp- Class definition main.cpp-main0 function (2) Build the ContactNode class per the following specifications Parameterized constructor. Parameters are name followed by phone number Public member functions InsertAfter0 (2 pts) GetName0 -Accessor(1 pt) GetPhoneNumber- Accessor (1 pt) GetNext0-Accessor (1 pt) PrintContactNode Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex....

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

  • C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and In...

    C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...

  • (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure...

    (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. ContactNode.h - Struct definition, including the data members and related function declarations ContactNode.c - Related function definitions main.c - main() function (2) Build the ContactNode struct per the following specifications: Data members char contactName[50] char contactPhoneNum[50] struct ContactNode* nextNodePtr Related functions CreateContactNode() (2 pt) InsertContactAfter() (2 pts) Insert...

  • Need help with this class and function please!!! #include <iostream> using namespace std; c...

    Need help with this class and function please!!! #include <iostream> using namespace std; class Team { string teamId; string name; string coach; Team *next; friend class teamlist; public: Team * getNext(); return next; void setNext(Team *r); next=r;    string getTeamId(); return teamId;    void setTeamId(string aTeamId); teamId = aTeamId;    string getName(); return name;    void setName(string aName); name = aName;    string getCoach(); return coach; void setCoach(string aCoach); coach = aCoach; } team list The team list is a dynamic linked list of team...

  • 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;...

  • Hello I need some help with my CC+ project. My current project erasing the first two...

    Hello I need some help with my CC+ project. My current project erasing the first two phone numbers and only printing the 3rd phone number out in the list. Any idea on how to correct. Here is the error. Contacts.h #ifndef Contact_H #define Contact_H #include<string> using namespace std; class ContactNode{ public: ContactNode(); ContactNode(string name, string phone); void InsertAfter(ContactNode*); string GetName(); string GetPhoneNumber(); ContactNode* GetNext(); void PrintContactNode(); private: string contactName; string contactPhoneNum; ContactNode* nextNodePtr; }; #endif Contacts.cpp #include <iostream> #include "Contacts.h"...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

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