Write in C++ please:
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;
}
comment down for any queries
please give a thumbs up
Write in C++ please: Write a class named MyVector using the following UML diagram and class...
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 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 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 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 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 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; 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 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 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 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:...