Question
Chapter 7 (Gaddis 18) introduces you to linked lists. In this lab assignment you will use the algorithms presented in this chapter to implement a
Chapter 7 (Gaddis 18) introduces you to linked lists. In this lab assignment you will use the algorithms presented in this chapter to implement a phone book that keeps a list of names with respective phone numbers. The contacts should be kept in alphabetical order by their last name, then first name. No repetitions are allowed.
You will adapt the Linked List Operations presented on Chapter 7 to work with a list of names and phone numbers. The header file is provided here: Contact.h
Please note that list abstract data type on the zyBook has two pointers: head and tail. You only need head.
The class that implements the phone book must have the following member functions:
- Constructor: should initialize the head to null.
- Destructor: should clean up the list. Note that this method must follow the algorithm that eliminates one node at a time. If you just set head to null, you create a memory leak in your program.
- Insert: must create a new node (with the name and phone number passed as parameters) and insert it in the list keeping it in alphabetical order.
- Delete: receives the name as a parameter, looks for the node that matches the name and removes it from the list.
- Print: traverses the list printing each contact (name and phone number)
Turn in:
contactlist.cpp (with implementation of all member functions) and main.cpp (with the logic of the phone book operations).
Output:
Take a screen shot of the testing you did. Insert at least 3 contacts, printing in between, remove each one of them, printing in between.
Contact.h
#ifndef Contact_hpp #define Contact_hpp #includeusing namespace std; // structure to hold the data related to one contact struct Contact { string firstName; string lastName; string tel; }; // This operator is being overloaded so it can be used to print the content of // a variable of type Contact. // Ex: Contact person // cout << person inline ostream &operator << (ostream &strm, const Contact &obj) { strm << obj.firstName + "\t"; strm << obj.lastName + "\t"; strm << obj.tel; return strm; } // Definition of one node on the linked list class Node { public: Node *next; Contact data; // Constructor initializes next and data Node(const Contact &value) : next(nullptr), data(value) { } // This function should Display the information in one node void display() { } }; // Definition of the linked list class List { private: Node *head; // points to the beginning of the list public: List() : head(nullptr) { } // The destructor should traverse the list destroying each node ~List() { } // this function should insert in a way that keeps the list alphabatically organized void insert(const Contact value) { } // this function should delete a contact from the list void deleteContact(std::string name) { } // this function should prints the whole list void print() const { } }; #endif /* Contact_hpp */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started