In: Computer Science
C++ program:
Define a structure to hold the contact's information including: name, phone number, and a pointer to the next node on the list. Each node on the list will be a contact instead of a number (like the example in the book).
struct ContactNode
{
string name;
string phoneNumber;
ContactNode *next;
}
You can find an implementation for those operations in the book and slides.
Your main program should instantiate an object of the Names List and ask the user to enter a list of names (first and last), which you will insert in alphabetical order by calling the appropriate method. When the user is done entering the names, call the displayList method to display the names, which should be in alphabetical order.
Turn in the list implementation in a header file and main.cpp
main.cpp file:
#include <iostream>
#include <string>
#include "contact.h"
using namespace std;
int main() {
    // welcome message
   cout << "Welcome to Contact List Program!"
<< endl;
   // create Contact list object
   ContactList list;
   // while loop to enter list of contacts
   while (true) {
       //ask user to enter new contact or
exit
       cout << "Enter new Contact in
list(enter -1 to exit)" << endl;
       cout << "Enter Name (first
and last): ";
       string name; //string to hold name
of contact
       getline(cin, name); //get user
input
       // check if user entered "-1"
       if (name.compare("-1") == 0)
{
           break; // break
the loop as user done entering contacts
       }
       else {
           // ask user to
enter phone number
           cout <<
"Enter Phone Number: ";
           string number;
//string to hold phone number
           getline(cin,
number); //get user input
           // insert
contact into the list
           list.add(name,
number);
       }
   }//end of loop
   // print list befor program exit
   list.print();
}
contact.h (header) file with implimentation:
#pragma once
#include <iostream>
using namespace std;
struct ContactNode {
   string name;
   string phoneNumber;
   ContactNode* next;
};
class ContactList {
private:
   ContactNode* head;
public:
   ContactList(); //constructor
   ~ContactList(); //destructor
   void add(string name, string phoneNumber); //function
to add contact to list in sorted order
   void print(); //print all contacts in list
};
ContactList::ContactList() {
   // initialize private member
   head = nullptr;
}
ContactList::~ContactList() {
   // destructor to delete list
   // loop through list and delete one node at a
time
   while (head != nullptr) {
       //create temparary node to delete
the node and free memory
       ContactNode* temp = head; //assign
temp to head(current node)
       head = head->next; //move head
to next node
       delete temp; //delete current
node
   }
}
void ContactList::add(string _name, string _phoneNumber) {
   // function to add contact in the list in alphabetical
order by name
   // create a new contactnode
   ContactNode* node = new ContactNode();
   node->name = _name; //initialize name
   node->phoneNumber = _phoneNumber; //initialize
phone number
   node->next = nullptr; //set next of node to
nullptr
   //check if head is nullptr
   if (head == nullptr) {
       // set new node to head
       head = node;
   }
   else {
       // loop through the list and insert
node at proper location in the list
       // create iterator to loop the
list
       ContactNode* i = head; //set
iterator to start of list
       bool inserted = false; //create
flag to check that node is not inserted
       while (i->next != nullptr)
{
           // loop till end
of list
           // check for
current node's name if greater then new node's name
           if
(i->name.compare(node->name) > 0) { //this can only happen
when current node is first node(head)
          
    // insert new node at start of the list
          
    node->next = head;
          
    head = node; //reset head to start of the
list
          
    inserted = true; //set flag to true
          
    break; //break the loop as node insertion is
done
           }
           else {
          
    // check for next node's name
          
    if (i->next->name.compare(node->name)
> 0) {
          
        // if next node's name is
greater then new node's name then
          
        // insert new node between
current node and next node
          
        node->next = i->next;
//set new node's next to next node
          
        i->next = node; //set
current node's next to new node
          
        inserted = true; //set flag
to true
          
        break; //break the while loop
as node is inserted in thee list
          
    }
          
    else {
          
        // when next node's name is
less then new node's name, move forword in the list
          
        i = i->next; //set
iterator to next node
          
    }
           }
       }
       // if node is not inseted till end
of the list
       if (!inserted) {
           // check for
current node's name if greater then new node's name
           if
(i->name.compare(node->name) > 0) { //this can only happen
when current node is first node(head)
          
    // insert new node at start of the list
          
    node->next = head;
          
    head = node; //reset head to start of the
list
           }
           else {
          
    // insert the node at end of the list
          
    i->next = node;
           }
       }
   }
}
void ContactList::print() {
   // function to print all contact in alphabetical order
from the list
   // print line indicating print function
   cout << "The Contact List is :" <<
endl;
   // create iterator to loop the list
   ContactNode* itr = head; //set iterator to start of
list
   // loop till itr is nullptr
   while (itr != nullptr) {
       cout << "Name: " <<
itr->name << endl; //print name
       cout << "Phone Number: "
<< itr->phoneNumber << endl; //print phone
number
       cout << endl; //create empty
line to seprate contacts from each other
       itr = itr->next; //move iterator
to next node
   }
}




