Question

In: Computer Science

C++ program: Define a structure to hold the contact's information including: name, phone number, and a...

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;
}
  1. Define a class containing the structure, private member variable head (that will point to the beginning of the list) and the following member functions:
    • A constructor that will initialize the head
    • A destructor that will destroy the list. This method needs to delete one node at a time.
    • A method to add contacts to the list. This method must insert a node at the right place in order to keep the list sorted.
    • A method to traverse the list to print all contacts

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

Solutions

Expert Solution

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
   }
}


Related Solutions

Programming C: Write a program for a Rolodex of contact information (e.g., name, phone number, email)...
Programming C: Write a program for a Rolodex of contact information (e.g., name, phone number, email) implemented as a linked list. The program will ask the user to enter a new contact information, retrieve/print a person’s contact information, and to delete a person. It will maintain the linked list in alphabetical order by last name. It will also allow the user to search for a person’s contact information by last name. Assume that all last names are unique.
Goals Understand class structure and encapsulation Description Define a class Product to hold the name and...
Goals Understand class structure and encapsulation Description Define a class Product to hold the name and price of items in a grocery store. Encapsulate the fields and provide getters and setters. Create an application for a grocery store to calculate the total bill for each customer. Such program will read a list of products purchased and the quantity of each item. Each line in the bill consists of: ProductName ProductPrice Quantity/Weight The list of items will be terminated by “end”...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information of COVID 19 cases of countries around the world. It keeps the name of the country, number of COVID 19 cases, number of deaths, current test positive rate, etc. After defining the structure, declare a variable of Covid19Info type with some initial values for Bangladesh. [8]
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
a) Write C code using a struct to hold student information: integer id integer number of...
a) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa b) create a variable of the type in #25 and initialize it with some data.
Write a C program for a library automation which gets the ISBN number, name, author and...
Write a C program for a library automation which gets the ISBN number, name, author and publication year of the books in the library. The status will be filled by the program as follows: if publication year before 1985 the status is reference else status is available. The information about the books should be stored inside a linked list. The program should have a menu and the user inserts, displays, and deletes the elements from the menu by selecting options....
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
Using the given file, ask the user for a name, phone number, and email. Display the...
Using the given file, ask the user for a name, phone number, and email. Display the required information. These are the Files that I made: import java.util.Scanner; public class Demo5 { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("New number creation tool"); System.out.println("Enter name"); String name = keyboard.nextLine(); System.out.println("Enter phone number"); String phoneNumber = keyboard.nextLine(); System.out.println("Enter email"); String email = keyboard.nextLine(); Phone test1 = new SmartPhone(name, phoneNumber, email); System.out.print(test1); System.out.println("Telephone neighbor: " + ((SmartPhone) test1).getTeleponeNeighbor()); }...
Write a C++ program to display toy name
Write a C++ program to display toy name
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT