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

Program must be written in C++: The purpose of this lab is to practice working w

ID: 3840034 • Letter: P

Question

Program must be written in C++:

The purpose of this lab is to practice working with circular Linked Lists. Complete the program below that keeps track of a telephone directory.

Use the comments below to implement the routines that are not implemented. #include #include using namespace std; class CLL; class Node { friend class CLL; private: string firstName; string lastName; string phoneNumber; Node * nextRecord; }; class CLL { private: Node * head; public: CLL(); CLL(const CLL&); // optional ~CLL(); void append(string, string, string); void insertAtBeginning(string, string, string); void print(); void searchByName(string, string); void removeRecord(string, string); }; //-------------------------------------------- // the default constructor CLL::CLL() { } //-------------------------------------------- // the copy constructor: optional CLL::CLL(const CLL& source) { } //-------------------------------------------- // the destructor CLL::~CLL() { } //-------------------------------------------- // print the linked list void CLL::print () { } //-------------------------------------------- // search for a particular person in the list and print the // corresponding phone number void CLL::searchByName (string fName, string lName) { } //-------------------------------------------- // create a node and insert the node on the top of the // linked list before the first node. void CLL::insertAtBeginning (string fName, string lName, string phone) { } //-------------------------------------------- // create a node and appends the node at the end of the // linked list after the last node. void append(string fName, string lName, string phone) { } //-------------------------------------------- // remove a person's record from the list void CLL::removeRecord (string fName, string lName) { } //-------------------------------------------- int main () { CLL list1; list1.append ("Mayssaa", "Najjar", "878-635-1234"); list1.insertAtBeginning ("Jim", "Meyer", "337-465-2345"); list1.append ("Joe", "Didier", "352-654-1983"); list1.insertAtBeginning("Nancy", "Garcia", "617-227-5454"); list1.print(); list1.searchByName ("Nancy", "Garcia"); list1.removerecord ("Mayssaa", "Najjar"); list1.print(); // this part is optional CLL list2(list1); list2.print(); return 0; }

Explanation / Answer

Following is the required C++ code :

#include <iostream>
using namespace std;


class CLL;

class Node {
   friend class CLL;
public:
   string firstName;
   string lastName;
   string phoneNumber;
   Node * nextRecord;
};

class CLL{
private:
   Node* head;
public:
   CLL();
   CLL(const CLL&);
   // optional ~CLL();
   Node* getHead();
   void append(string, string, string);
   void insertAtBeginning(string, string, string);
   void print();
   void searchByName(string, string);
   void removeRecord(string, string); };
//--------------------------------------------


// the default constructor CLL::CLL() { }
CLL::CLL(){
   head = NULL;
}
//--------------------------------------------


// the copy constructor: optional CLL::CLL(const CLL& source) { }
CLL::CLL( const CLL& source){
   head = source.head;
}
//--------------------------------------------

Node* CLL::getHead(){
   return head;
}

// the destructor CLL::~CLL() { }
//--------------------------------------------

// print the linked list void CLL::print () { }
void CLL::print(){
   Node* current = head;
   while( current != NULL ){
       cout << current->firstName << " " << current->lastName << " " << current->phoneNumber << endl;
       current = current->nextRecord;
   }
}
//--------------------------------------------

// search for a particular person in the list and print the
// corresponding phone number void CLL::searchByName (string fName, string lName) { }
void CLL::searchByName( string fName, string lName ){
   Node* current = head;
   while( current != NULL ){
       if(( current->firstName == fName ) and (current->lastName == lName )){
           cout << "Found the person and the phone number is : " << current->phoneNumber << endl;
           return;
       }
       current = current->nextRecord;
   }
}
//--------------------------------------------


// create a node and insert the node on the top of the
// linked list before the first node. void CLL::insertAtBeginning (string fName, string lName, string phone) { }
void CLL::insertAtBeginning (string fName, string lName, string phone){
   Node* newNode = new Node;
   newNode->firstName = fName;
   newNode->lastName = lName;
   newNode->phoneNumber = phone;
   newNode->nextRecord = head;
   head = newNode;
}
//--------------------------------------------

// create a node and appends the node at the end of the
// linked list after the last node.
void CLL::append(string fName, string lName, string phone){
   Node* newNode = new Node;
   newNode->firstName = fName;
   newNode->lastName = lName;
   newNode->phoneNumber = phone;
   if( head == NULL ){
       head = newNode;
       return;
   }
   Node* current = head;
   while( current->nextRecord != NULL )
   {
       current = current->nextRecord;
   }
   current->nextRecord = newNode;
}
//--------------------------------------------

// remove a person's record from the list
void CLL::removeRecord (string fName, string lName){
   if( head == NULL ){ return; }
   //it is head ?
   if( head->firstName == fName and head->lastName == lName ){
       Node* current = head->nextRecord;
       delete head;
       head = current;
       return;
   }
   //it is other nodes
   Node* current = head->nextRecord;
   Node* previous = head;
   while( current != NULL ){
       if( current->firstName == fName and current->lastName== lName ){
           previous->nextRecord = current->nextRecord;
           delete current;
           break;
       }
       previous = current;
       current = current->nextRecord;
   }
   return;
}
//--------------------------------------------


int main(){
   CLL list1;
   list1.append ("Mayssaa", "Najjar", "878-635-1234");
   list1.insertAtBeginning ("Jim", "Meyer", "337-465-2345");
   list1.append ("Joe", "Didier", "352-654-1983");
   list1.insertAtBeginning("Nancy", "Garcia", "617-227-5454");
   list1.print();
   list1.searchByName ("Nancy", "Garcia");
   list1.removeRecord ("Mayssaa", "Najjar");
   list1.print();
   // this part is optional CLL list2(list1); list2.print();
   return 0;
}