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
Implement a Linked List Node: Define a
ContactNode
class representing a node in the linked list.Build the Linked List: Create a linked list by connecting multiple
ContactNode
objects.Traverse the Linked List: Implement functionality to traverse and display the list's contents.
Memory Management: Ensure proper allocation and deallocation of memory.
Tasks
Create the
ContactNode
Class:Define the class in
ContactNode.hpp
orContactNode.h
depending on your compilerImplement the class methods in
ContactNode.cpp
.
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.
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
Create a file named
ContactNode.hpp
.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
Create a file named
ContactNode.cpp
.Include the
ContactNode.hpp
header file.Implement the constructor of the
ContactNode
class to initialize the contact's name, phone number, and the next node pointer.Implement the destructor.
Implement the member functions
GetName
,GetPhoneNumber
,InsertAfter
,GetNext
,PrintContactNode
, andPrintRestOfList
as defined in the header file.
main.cpp
Create a file named
main.cpp
.Include the
ContactNode.hpp
header file and the<iostream>
library.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
Once the program starts, it should prompt you to enter the name and phone number for three contacts.
After entering the details for each contact, the program should display the list of contacts with their respective details.
Verify that the contacts are displayed in the order they were entered.
Example Run:
Deliverable
Upload the following:
Full source code (.cpp files or .txt files)
Screenshot of the Console with the code executing
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark