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

Use the following UML diagram and method descriptions to write a class implement

ID: 3594108 • Letter: U

Question

Use the following UML diagram and method descriptions to write a class implementing an AddressBook. Unlike the previous assignment, this version will be implemented using a linked list instead of an array.

As before, put it in it's own header file named AddressBook.h.

Class Attributes:

Contact - this is a nested struct. Name and phone are both type string and store the name and phone number of a contact. list is the "head pointer" for the list. It holds the memory address of the first Contact in the list.

AddressBook - the constructor sets list to null.

~AddressBook - the destructor frees the memory allocated by the AddressBook object.

isEmpty - returns true if list is null, false otherwise.

isFull - checks to see if there is enough free memory to create a new Contact. It does this by creating a dummy (a temporary one you delete) Contact using dynamic memory allocation. If successful, it deletes the Contact and then returns true, returns false otherwise.

add - accepts the name and phone number of a new contact to add to the list. Stores the information in a dynamically allocated Contact and appends it to the list.

remove - accepts the name of a contact, searches the list for the name, and removes the first matching Contact in the list if found.

clear - frees up all memory created by the AddressBook object and sets list to null.

find - accepts the name of a contact as it's only argument. It searches the Contacts list and returns the phone number of the first matching Contact. If none are found, returns NONE FOUND instead.

Hints:

In a linked list, we have a bunch of Nodes strung together with pointers. In AddressBook, Contacts are our Nodes.

Want to save some time? Have the destructor call clear.

Notice the attached test driver is the exact same test driver from the first assignment.

Here is the testDrive.cpp for this question

i need an answer for this question thanks.

Explanation / Answer

Given below is the needed code and output of the test program. Please do rate the answer if it helpe.d Thank you.

AddressBook.h
#ifndef AddressBook_h
#define AddressBook_h
#include <iostream>
using namespace std;
struct Contact
{
string name;
string phone;
struct Contact* next;
};

class AddressBook
{
private:
struct Contact* head;
public:
AddressBook();
void add(string name, string phone);
bool remove(string name);
void clear();
bool isEmpty() const;
bool isFull() const;
string find(string name);
~AddressBook();
};

#endif /* AddressBook_h */

AddressBook.cpp

#include "AddressBook.h"
AddressBook::AddressBook()
{
head = NULL;
}
void AddressBook::add(string name, string phone)
{
Contact* curr = head;
Contact* n = new Contact();
n->name = name;
n->phone = phone;
n->next = NULL;
  
if(head == NULL)//empty
head = n;
else
{
while(curr->next != NULL)
curr = curr->next;
  
curr->next = n;
}
}
bool AddressBook::remove(string name)
{
Contact* curr = head;
Contact* prev = NULL;
  
if(head == NULL)
return false;
else
{
while(curr != NULL)
{
if(curr->name == name)
break;
  
prev = curr;
curr = curr->next;
}
  
if(curr == NULL)
return false;
  
if(curr == head ) //removing head
head = curr->next;
else
prev->next = curr->next;
delete curr;
return true;
}
}
void AddressBook::clear()
{
Contact* curr = head;
Contact* next;
  
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
head = NULL;
}
bool AddressBook::isEmpty() const
{
return head == NULL;
}
bool AddressBook::isFull() const
{
Contact *temp = new Contact;
bool full = false;
if(temp == NULL)
full = true;
delete temp;
return full;
}

string AddressBook::find(string name)
{
Contact* curr = head;
while(curr != NULL)
{
if(curr->name == name)
return curr->phone;
curr = curr->next;
}
return "Not Found" ;
}
AddressBook::~AddressBook()
{
clear();
}

output

A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> a
Enter contact's name: john
Enter contact's number: 1111
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> a
Enter contact's name: bob
Enter contact's number: 222
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> f
Enter contact's name: bob
222
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> f
Enter contact's name: john
1111
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> f
Enter contact's name: jake
Not Found
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> r
Enter contact's name: john
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> f
Enter contact's name: john
Not Found
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> e
AddressBook isn't empty.
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> u
AddressBook isn't full.
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> c
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> e
AddressBook is empty.
A-dd Contact
R-emove Contact
F-ind Contact
C-lear Contacts
E-isEmpty
U-isFull
Q-uit
> q

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