Instructions
Objective
Write a C++ assignment program to implement nodes area.
Requirements and Specifications
Description : In this assignment you will write a program for a phone company. Your program will use a list of lists to maintain the database of phone numbers. The main list will refer to the area codes. The lists coming off of the main list will store the telephone numbers in that area code. Each phone number should store the prefix and suffix separately. Your program should be able to support the following actions: Add a phone number to the list. If necessary, a new area code will be created. For example, using the above list, adding the phone number 817-458-3101 would add a new node to the 817 area code list. Adding the phone number 350-921-6749 would add a new area code node of 350 and then off of that list, the phone number 921-6749 will be added. Be sure not to add duplicate numbers. Each new area code added will be added to the front of the list of area codes and each new phone number will be added to the front of the list of phone numbers. Remove a phone number from the list. If removing the phone number causes the corresponding area code to have an empty list, then you should also remove the area code node. Search for a given phone number Count how many phone numbers there are in the list Count how many phone numbers there are in a given area code Count how many phone numbers there are in a given area code and a given prefix. Split an area code. For example, move all the phone numbers with the 353 prefix in the 909 area code to a new area code of 650. The new area code should not already exist and should be added to the head of the area code list. If splitting causes the old area code to have an empty list of phone numbers, the old area code should also be removed. There are many List templates available (for example, the Standard Template Library offers a list template class). However, for this assignment, when you use a linked list, it will be your own code so that you can practice your programming and learn the concepts thoroughly. You may however, use any variation of a linked list (double linked list, sentinels, etc) as long as it interfaces with the main functions provided. Skeleton code provided DO NOT alter any of the code provided to you. Please only ADD to the code. The files can be found in the Doc Share. Structure Your program will consist of 3 basic classes and I have provided you with the skeleton code for those classes in the Doc Share. DO NOT make any additional classes. You only need these 3 classes. You must download these files and complete the code in them. phone_book.h - The first class will be the phonebook list class which will contain a pointer to the head of your area code nodes. phone_book.cpp - You are also being provided the cc file for the phonebook class because it contains the print function that has been written for you. DO NOT change this print function. However, note how the print functions are structured. I have a print function in the phonebook class that only prints the area codes for the lists. Then it calls a print function within the area_node class that prints out the phone numbers in the area code. The phone numbers are hidden from the phonebook class. You should follow this model with EVERY function that you write. area_node.h - The second class will be an area code node that will hold the current area code, a pointer to the next area code in the list, and a pointer to the head of the list of phone numbers in that area code. area_node.cpp - You are also being provided the cc file for the area node class because it contains the print function that has been written for you. DO NOT change this print function. number_node.h - Finally, you will have a phone number class which will hold the current phone number and a pointer to the next phone number in the list. main.cpp - This is the main function for the complete assignment that we will be testing your programs with. It contains function calls that test the code that you write. Since this is THE file we will be using for testing, you know how well you will do on the assignment because you will know how well you perform on the test cases. The output of your program should look like output.txt. DO NOT add anything to the main files that is necessary for your program to compile/function correctly. Function prototypes You will write the following functions for your PhoneBook class to interface with the main function. This is in no way an inclusive list of the functions that you will need to write. This is simply the minimum amount of information that I need to give you so that your code will interface with the main testing function. For each function, please follow the specifications listed above. void removePhoneNumber ( int area, int prefix, int suffix ) - This function will remove a phone number from the phone book. The function parameters are: area - the area code of the number to be removed prefix - the prefix of the number to be removed suffix - the suffix of the number to be removed void insertPhoneNumber ( int area, int prefix, int suffix ) - This function will insert a phone number into the phone book. The function parameters are: area - the area code of the number to be inserted prefix - the prefix of the number to be inserted suffix - the suffix of the number to be inserted bool search ( int area, int prefix, int suffix ) - This function will search for a phone number in the phone book. The function will return true if the phone number is found and false if the phone number is not found. The function parameters are: area - the area code of the number to search for prefix - the prefix of the number to search for suffix - the suffix of the number to search for int numNumbers ( ) - This function will return the total number of phone numbers in the phone book. int numAreaCodeNumbers ( int area ) - This function will return the total number of phone numbers in the specified area code. The function parameters are: area - the area code in which to count phone numbers in int numAreaCodeAndPrefixNumbers ( int area, int prefix ) - This function will return the total number of phone numbers in the specified area code that belong to a specified prefix. The function parameters are: area - the area code in which to count phone numbers in prefix - the prefix in which to count phone numbers in void split ( int oldAreaCode, int prefix, int newAreaCode ) - This function will take phone numbers from a given prefix in a given area code and place them into a new area code. The function parameters are: oldAreaCode - the area code which contains the phone numbers that will be moved to a new area code prefix - the prefix of the phone numbers that will be moved to the new area code newAreaCode - the area code that the phone numbers will be moving to You must also write the following functions for the AreaNode class (not a complete list). int size ( ) - this function will return the number of phone numbers in the list for this area code. This function is important because it is used by the print function to nicely print the phone numbers to the screen. bool isEmpty ( ) - returns true if the areaNode list is empty, false if the list isn't empty. void removeNumber ( int prefix, int suffix ) - This function will remove a phone number from the areaNode list. The function parameters are: prefix - the prefix of the number to be removed suffix - the suffix of the number to be removed void insertNumber ( int prefix, int suffix ) - This function will insert a phone number into the areaNode list. The function parameters are: prefix - the prefix of the number to be inserted suffix - the suffix of the number to be inserted NumberNode* search ( int prefix, int suffix ) - This function will search for a phone number in the areaNode list. The function will return a pointer to the node if the phone number is found and NULL if the phone number is not found. The function parameters are: prefix - the prefix of the number to search for suffix - the suffix of the number to search for int numPrefixNumbers ( int prefix ) - This function will return the total number of phone numbers that belong to a specified prefix. The function parameters are: prefix - the prefix in which to count phone numbers in Error checking Please make sure to provide good error checking throughout. If an error occurs, i.e inserting a duplicate number or deleting a number that does not exist, print out an error message and return from the function. Do not exit! Make sure that your error message matches mine the in sample output EXACTLY. Notes on grading Your programs will be graded with main.cc. You will receive points for the test cases that your code passes. If your code does not interface with main functions provided, you will not receive any functionality points. DO NOT, I repeat, DO NOT add anything to the main files that is necessary for your program to compile/function correctly. Much of the grading of your program will be based on not just getting code to do the necessary functions correctly, but on your actual coding. Be sure to use good programming where possible (use of classes, functions, good parameter passing techniques, error checking, encapsulation, object oriented programming). Make sure your program is user friendly. Suggestions to follow while coding Follow a modular programming style. Write one function for your program and completely test it before moving to the next function. This will isolate your debugging because most of the time the bug will be in the newly created module. However, if you forget a test case in an old module and do not completely test it, a new module can reveal old bugs. Watch out for this. Start working on the AreaNode class FIRST! Figure out how to insert a phone number into an AreaNode's List, how to search an AreaNode's List, and how to count an AreaNode's List (how many nodes are in the list?). You will need to modify the main.cpp file so you can test it out. Get AreaNode working first. AreaNode's class declaration is missing a lot of functions you'll need. In total, I created 10 additional member functions in AreaNode in the final solution (parts 1 and 2). What to turn in For Part 1, you need to create and implement the AreaNode list storing NumberNodes. For Part 2, you'll implement the phonebook class.
Source Code
#include
#include "area_node.h"
using namespace std;
#define NUM_TO_PRINT_PER_LINE 5
//-----------------------------------------------------------------------
// DO NOT MODIFY THIS PRINT FUNCTION
// BIG-O RUN TIME OF THIS FUNCTION: O(n)
void AreaNode::print ( )
{
NumberNode* temp = head;
for ( int x = 0; x < size ( ) && temp != NULL; x += NUM_TO_PRINT_PER_LINE )
{
cout << " " << flush;
for ( int y = 0; y < NUM_TO_PRINT_PER_LINE && temp != NULL; y ++, temp = temp->next )
{
cout << temp->prefix << "-" << flush << temp->suffix << ", " << flush;
}
cout << endl;
}
}
//-----------------------------------------------------------------------
// BIG-O RUN TIME OF THIS FUNCTION: O(n)
int AreaNode::size ( )
{
int count = 0;
NumberNode *current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
//-----------------------------------------------------------------------
// Constructor
AreaNode::AreaNode(int areaCode)
{
this->areaCode = areaCode;
next = NULL;
head = NULL;
}
// Returns true if the areaNode list is empty, false if the list isn't empty
// BIG-O: O(1)
bool AreaNode::isEmpty()
{
return head == NULL;
}
// Remove a phone number from the areaNode list.
// Prefix - the prefix of the number to be removed
// Suffix - the suffix of the number to be removed
// BIG-O: O(n)
void AreaNode::removeNumber(int prefix, int suffix)
{
NumberNode *previous = NULL;
NumberNode *current = head;
while (current != NULL)
{
if (current->prefix == prefix && current->suffix == suffix)
{
if (current == head)
head = head->next;
else
previous->next = current->next;
delete current;
return;
}
previous = current;
current = current->next;
}
cout << "Error: Phone number does not exist" << endl;
}
// Insert a phone number into the areaNode list.
// Prefix - the prefix of the number to be inserted
// Suffix - the suffix of the number to be inserted
// BIG-O: O(1)
void AreaNode::insertNumber(int prefix, int suffix)
{
if (search(prefix, suffix) != NULL)
return;
NumberNode *number = new NumberNode(prefix, suffix);
number->next = head;
head = number;
}
// Search a phone number in the areaNode list.
// Return the pointer to the node if the phone number is found and NULL
// if the phone number is not found
// Prefix - the prefix of the number to search for
// Suffix - the suffix of the number to search for
// BIG-O: O(n)
NumberNode *AreaNode::search(int prefix, int suffix)
{
NumberNode *current = head;
while (current != NULL)
{
if (current->prefix == prefix && current->suffix == suffix)
return current;
current = current->next;
}
return NULL;
}
// Return the total number of phone numbers that belong to a specified prefix
// Prefix - the prefix in which to count phone numbers in
// BIG-O: O(n)
int AreaNode::numPrefixNumbers(int prefix)
{
int count = 0;
NumberNode *current = head;
while (current != NULL)
{
if (current->prefix == prefix)
count++;
current = current->next;
}
return count;
}
// Delete all number nodes
// BIG-O: O(n)
AreaNode::~AreaNode()
{
NumberNode *current = head;
while (current != NULL)
{
NumberNode *next = current->next;
delete current;
current = next;
}
}
Related Samples
Explore our free C++ assignment samples for a clear perspective on tackling programming challenges. These samples offer detailed solutions and practical examples, demonstrating C++ concepts in real-world scenarios. By reviewing these samples, you can gain valuable insights and approaches to effectively handle your own assignments.
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++