Lab 2 - Linked Lists - Contacts

 Objective

In this lab, you will implement a simple linked list to manage a list of contacts. Each contact will have a name and a phone number. The primary focus is understanding how linked lists function, how to manipulate them, and the concepts of dynamic memory allocation in C++.

Objectives

  1. Implement a Linked List Node: Define a ContactNode class representing a node in the linked list.

  2. Build the Linked List: Create a linked list by connecting multiple ContactNode objects.

  3. Traverse the Linked List: Implement functionality to traverse and display the list's contents.

  4. Memory Management: Ensure proper allocation and deallocation of memory.

Tasks

  1. Create the ContactNode Class:

    • Define the class in ContactNode.hpp or ContactNode.h depending on your compiler

    • Implement the class methods in ContactNode.cpp.

  2. Implement Main Functionality:

    • In main.cpp, write a program that allows the user to enter contact information for three contacts.

    • Build the linked list by connecting these nodes.

    • Traverse and print the list to display the contacts.

  3. Test Your Implementation:

    • Compile and run your program.

    • Ensure it correctly prompts for input, builds the list, and displays the contacts in order.

    • Check for memory leaks or errors.

Instructions

ContactNode.hpp

  1. Create a file named ContactNode.hpp.

  2. Define a class ContactNode that will represent a node in a linked list. This class should have the following:

    • Private member variables for the contact's name (string contactName), phone number (string contactPhoneNumber), and a pointer to the next node (ContactNode* nextNodePtr).

    • A constructor that initializes the contact's name, phone number, and next node pointer. Set default values in case no data is provided.

    • A destructor (even if it's empty).

    • Member functions for getting the contact's name (GetName), phone number (GetPhoneNumber), inserting a node after the current node (InsertAfter), getting the next node (GetNext), printing the contact's details (PrintContactNode), and printing all nodes starting from the current node (PrintRestOfList).

ContactNode.cpp

  1. Create a file named ContactNode.cpp.

  2. Include the ContactNode.hpp header file.

  3. Implement the constructor of the ContactNode class to initialize the contact's name, phone number, and the next node pointer.

  4. Implement the destructor.

  5. Implement the member functions GetName, GetPhoneNumber, InsertAfter, GetNext, PrintContactNode, and PrintRestOfList as defined in the header file.

main.cpp

  1. Create a file named main.cpp.

  2. Include the ContactNode.hpp header file and the <iostream> library.

  3. In the main function, do the following:

    • Prompt the user to enter the name and phone number for three contacts.

    • Create a head node and two additional nodes for the linked list using the user's input.

    • Use the InsertAfter method to build the linked list by connecting the nodes.

    • Traverse the linked list starting from the head node and print the details of each contact using the PrintContactNode method.

    • Finally, deallocate the memory used by the nodes to prevent memory leaks.

Testing the Program

  1. Once the program starts, it should prompt you to enter the name and phone number for three contacts.

  2. After entering the details for each contact, the program should display the list of contacts with their respective details.

  3. Verify that the contacts are displayed in the order they were entered.

Example Run:

 

Deliverable

Upload the following:

  1. Full source code (.cpp files or .txt files)

  2. Screenshot of the Console with the code executing

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark