SlideShare a Scribd company logo
1 of 16
Download to read offline
#include
#include
#include
#include
using namespace std;
struct city
{
string name; // name of the city
city *next; // pointer to the next city
int numberMessages; // how many messages passed through this city
string message; // message we are sending accross
};
city* addCity(city *head, city *previous, string cityName );
city* deleteCity(city *head, string cityName);
city* deleteEntireNetwork(city *head);
city* searchNetwork(city *head, string cityName);
city* loadDefaultSetup(city *head);
void transmitMsg(city *head, string receiver, string filename);
void printPath(city *head);
void displayMenu();
city* handleUserInput(city *head);
/* Do NOT modify main function */
int main(int argc, char* argv[])
{
// pointer to the head of our network of cities
city *head = NULL;
head = handleUserInput(head);
printPath(head);
head = deleteEntireNetwork(head);
if (head == NULL)
cout << "Path cleaned" << endl;
else
printPath(head);
cout << "Goodbye!" << endl;
return 0;
}
/*
* Purpose: handle the interaction with the user
* Do NOT modify
*/
city* handleUserInput(city *head)
{
bool quit = false;
string s_input;
int input;
// loop until the user quits
while (!quit)
{
displayMenu();
// read in input, assuming a number comes in
getline(cin, s_input);
input = stoi(s_input);
switch (input)
{
// print all nodes
case 1: //rebuild network
head = loadDefaultSetup(head);
printPath(head);
break;
case 2: // print path
printPath(head);
break;
case 3: //message is read in from file
{
string cityReceiver;
cout << "Enter name of the city to receive the message: " << endl;
getline(cin, cityReceiver);
cout << "Enter the message to send: " << endl;
string message;
getline(cin, message);
transmitMsg(head, cityReceiver, message);
}
break;
case 4:
{
string newCityName;
string prevCityName;
cout << "Enter a new city name: " << endl;
getline(cin, newCityName);
cout << "Enter the previous city name (or First): " << endl;
getline(cin, prevCityName);
// find the node containing prevCity
city *tmp = NULL;
if(prevCityName !="First")
tmp = searchNetwork(head, prevCityName);
// add the new node
head = addCity(head, tmp, newCityName);
printPath(head);
}
break;
case 5: // delete city
{
string city;
cout << "Enter a city name: " << endl;
getline(cin, city);
head = deleteCity(head, city);
printPath(head);
}
break;
case 6: // delete network
head = deleteEntireNetwork(head);
break;
case 7: // quit
quit = true;
cout << "Quitting... cleaning up path: " << endl;
break;
default: // invalid input
cout << "Invalid Input" << endl;
break;
}
}
return head;
}
/*
* Purpose: Add a new city to the network
* between the city *previous and the city that follows it in the network.
*/
city* addCity(city *head, city *previous, string cityName )
{
city* add = new city; //to distinguish and create the new node, i named it add
add->name = cityName;
city* tmp = new city;
tmp=head;
if(previous == NULL){
add->next = head;
head = add;
}
while(tmp != NULL && previous != tmp){
tmp = tmp->next;
}
if(tmp == NULL){
if(head == NULL){ // if the head is null then it creates a node for the head
head = add;
add->next = NULL;
}
return head;
}
else{
add->next = tmp->next; //the pointer for the new node will be the next of the prev node
tmp->next = add; // the prev node will now point to the new node
}
return head;
}
/*
* Purpose: Search the network for the specified city and return a pointer to that node
*/
city *searchNetwork(city *ptr, string cityName)
{
city *tmp = new city;
tmp = ptr;
while(tmp != NULL && tmp->name != cityName){
tmp = tmp->next;
}
return tmp;
}
/*
* Purpose: deletes all cities in the network starting at the head city.
*/
city* deleteEntireNetwork(city *ptr)
{
city *tmp = new city;
while(ptr->next != NULL){
tmp = ptr->next;
cout << "deleting: " << ptr->name << endl;
delete ptr;
ptr = tmp;
} //stops at the last node, so we need to delete it and make ptr = null
cout << "deleting: " << ptr->name << endl;
delete ptr;
cout << "Deleted network" << endl;
// return head, which should be NULL
return ptr;
}
/* sends messages from file */
void transmitMsg(city *head, string receiver, string message)
{
city *sender = new city;
sender = head;
if(head == NULL)
{
cout << "Empty list" << endl;
return;
}
else{
while(sender != NULL && sender->name != receiver){
sender->numberMessages = sender->numberMessages + 1; //adds to the amount of times
the city has intercepted a message
sender->message = message; //the message will be changed everytime for every city
cout << sender->name << " [# messages passed: " << sender->numberMessages <<
"] received: " << sender->message << endl;
sender = sender->next; // moves on to the next city
} // when the destination city is found the same 3 lines of code above will run but will end in
termination of the function
sender->numberMessages = sender->numberMessages +1;
sender->message = message;
cout << sender->name << " [# messages passed: " << sender->numberMessages << "]
received: " << sender->message << endl;
}
}
/*
* Purpose: delete the city in the network with the specified name.
*/
city* deleteCity(city *head, string cityName)
{
city *tmp = new city; //will traverse the list
city *prev = new city; // will hold the previous city for tmp
tmp = head;
if(cityName == head->name){
tmp = head->next;
delete head;
head = tmp;
return head;
}
while(tmp != NULL && tmp->name != cityName){
prev = tmp;
tmp = tmp->next;
}
if(tmp == NULL){
cout << "City does not exist." << endl;
}
else{
prev->next = tmp->next; //changes the previous next pointer to skip the deleted node
delete tmp;
}
return head;
}
/*
* Purpose: prints the current list nicely
*/
void printPath(city *ptr)
{
cout << "== CURRENT PATH ==" << endl;
if (ptr == NULL){
cout << "nothing in path" << endl;
}
else{
city *tmp = new city; //to traverse the linked list
tmp = ptr;
while(tmp !=NULL){ //ends the traversal at the end of the list
cout << tmp->name << " -> ";
tmp = tmp->next;
}
cout << "NULL"<next;
addCity(head,tmp,"Denver");
tmp = tmp->next;
addCity(head,tmp,"Dallas");
tmp = tmp->next;
addCity(head,tmp,"Atlanta");
tmp = tmp->next;
addCity(head,tmp,"New York");
return head;
}
/*
* Purpose; displays a menu with options
*/
void displayMenu()
{
cout << "Select a numerical option:" << endl;
cout << "======Main Menu=====" << endl;
cout << "1. Build Network" << endl;
cout << "2. Print Network Path" << endl;
cout << "3. Transmit Message" << endl;
cout << "4. Add City" << endl;
cout << "5. Delete City" << endl;
cout << "6. Clear Network" << endl;
cout << "7. Quit" << endl;
}
Solution
#include
#include
#include
#include
using namespace std;
struct city
{
string name; // name of the city
city *next; // pointer to the next city
int numberMessages; // how many messages passed through this city
string message; // message we are sending accross
};
city* addCity(city *head, city *previous, string cityName );
city* deleteCity(city *head, string cityName);
city* deleteEntireNetwork(city *head);
city* searchNetwork(city *head, string cityName);
city* loadDefaultSetup(city *head);
void transmitMsg(city *head, string receiver, string filename);
void printPath(city *head);
void displayMenu();
city* handleUserInput(city *head);
/* Do NOT modify main function */
int main(int argc, char* argv[])
{
// pointer to the head of our network of cities
city *head = NULL;
head = handleUserInput(head);
printPath(head);
head = deleteEntireNetwork(head);
if (head == NULL)
cout << "Path cleaned" << endl;
else
printPath(head);
cout << "Goodbye!" << endl;
return 0;
}
/*
* Purpose: handle the interaction with the user
* Do NOT modify
*/
city* handleUserInput(city *head)
{
bool quit = false;
string s_input;
int input;
// loop until the user quits
while (!quit)
{
displayMenu();
// read in input, assuming a number comes in
getline(cin, s_input);
input = stoi(s_input);
switch (input)
{
// print all nodes
case 1: //rebuild network
head = loadDefaultSetup(head);
printPath(head);
break;
case 2: // print path
printPath(head);
break;
case 3: //message is read in from file
{
string cityReceiver;
cout << "Enter name of the city to receive the message: " << endl;
getline(cin, cityReceiver);
cout << "Enter the message to send: " << endl;
string message;
getline(cin, message);
transmitMsg(head, cityReceiver, message);
}
break;
case 4:
{
string newCityName;
string prevCityName;
cout << "Enter a new city name: " << endl;
getline(cin, newCityName);
cout << "Enter the previous city name (or First): " << endl;
getline(cin, prevCityName);
// find the node containing prevCity
city *tmp = NULL;
if(prevCityName !="First")
tmp = searchNetwork(head, prevCityName);
// add the new node
head = addCity(head, tmp, newCityName);
printPath(head);
}
break;
case 5: // delete city
{
string city;
cout << "Enter a city name: " << endl;
getline(cin, city);
head = deleteCity(head, city);
printPath(head);
}
break;
case 6: // delete network
head = deleteEntireNetwork(head);
break;
case 7: // quit
quit = true;
cout << "Quitting... cleaning up path: " << endl;
break;
default: // invalid input
cout << "Invalid Input" << endl;
break;
}
}
return head;
}
/*
* Purpose: Add a new city to the network
* between the city *previous and the city that follows it in the network.
*/
city* addCity(city *head, city *previous, string cityName )
{
city* add = new city; //to distinguish and create the new node, i named it add
add->name = cityName;
city* tmp = new city;
tmp=head;
if(previous == NULL){
add->next = head;
head = add;
}
while(tmp != NULL && previous != tmp){
tmp = tmp->next;
}
if(tmp == NULL){
if(head == NULL){ // if the head is null then it creates a node for the head
head = add;
add->next = NULL;
}
return head;
}
else{
add->next = tmp->next; //the pointer for the new node will be the next of the prev node
tmp->next = add; // the prev node will now point to the new node
}
return head;
}
/*
* Purpose: Search the network for the specified city and return a pointer to that node
*/
city *searchNetwork(city *ptr, string cityName)
{
city *tmp = new city;
tmp = ptr;
while(tmp != NULL && tmp->name != cityName){
tmp = tmp->next;
}
return tmp;
}
/*
* Purpose: deletes all cities in the network starting at the head city.
*/
city* deleteEntireNetwork(city *ptr)
{
city *tmp = new city;
while(ptr->next != NULL){
tmp = ptr->next;
cout << "deleting: " << ptr->name << endl;
delete ptr;
ptr = tmp;
} //stops at the last node, so we need to delete it and make ptr = null
cout << "deleting: " << ptr->name << endl;
delete ptr;
cout << "Deleted network" << endl;
// return head, which should be NULL
return ptr;
}
/* sends messages from file */
void transmitMsg(city *head, string receiver, string message)
{
city *sender = new city;
sender = head;
if(head == NULL)
{
cout << "Empty list" << endl;
return;
}
else{
while(sender != NULL && sender->name != receiver){
sender->numberMessages = sender->numberMessages + 1; //adds to the amount of times
the city has intercepted a message
sender->message = message; //the message will be changed everytime for every city
cout << sender->name << " [# messages passed: " << sender->numberMessages <<
"] received: " << sender->message << endl;
sender = sender->next; // moves on to the next city
} // when the destination city is found the same 3 lines of code above will run but will end in
termination of the function
sender->numberMessages = sender->numberMessages +1;
sender->message = message;
cout << sender->name << " [# messages passed: " << sender->numberMessages << "]
received: " << sender->message << endl;
}
}
/*
* Purpose: delete the city in the network with the specified name.
*/
city* deleteCity(city *head, string cityName)
{
city *tmp = new city; //will traverse the list
city *prev = new city; // will hold the previous city for tmp
tmp = head;
if(cityName == head->name){
tmp = head->next;
delete head;
head = tmp;
return head;
}
while(tmp != NULL && tmp->name != cityName){
prev = tmp;
tmp = tmp->next;
}
if(tmp == NULL){
cout << "City does not exist." << endl;
}
else{
prev->next = tmp->next; //changes the previous next pointer to skip the deleted node
delete tmp;
}
return head;
}
/*
* Purpose: prints the current list nicely
*/
void printPath(city *ptr)
{
cout << "== CURRENT PATH ==" << endl;
if (ptr == NULL){
cout << "nothing in path" << endl;
}
else{
city *tmp = new city; //to traverse the linked list
tmp = ptr;
while(tmp !=NULL){ //ends the traversal at the end of the list
cout << tmp->name << " -> ";
tmp = tmp->next;
}
cout << "NULL"<next;
addCity(head,tmp,"Denver");
tmp = tmp->next;
addCity(head,tmp,"Dallas");
tmp = tmp->next;
addCity(head,tmp,"Atlanta");
tmp = tmp->next;
addCity(head,tmp,"New York");
return head;
}
/*
* Purpose; displays a menu with options
*/
void displayMenu()
{
cout << "Select a numerical option:" << endl;
cout << "======Main Menu=====" << endl;
cout << "1. Build Network" << endl;
cout << "2. Print Network Path" << endl;
cout << "3. Transmit Message" << endl;
cout << "4. Add City" << endl;
cout << "5. Delete City" << endl;
cout << "6. Clear Network" << endl;
cout << "7. Quit" << endl;
}

More Related Content

Similar to #include iostream #include fstream #include cstdlib #.pdf

So here is the code from the previous assignment that we need to ext.pdf
So here is the code from the previous assignment that we need to ext.pdfSo here is the code from the previous assignment that we need to ext.pdf
So here is the code from the previous assignment that we need to ext.pdf
leolight2
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docx
Jason0x0Scottw
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
amitbagga0808
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
MARRY7
 
Help with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfHelp with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdf
ezzi97
 
PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf
 PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf
PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf
annamalaiagencies
 
#include iostream#include string#include fstreamusi.docx
#include iostream#include string#include fstreamusi.docx#include iostream#include string#include fstreamusi.docx
#include iostream#include string#include fstreamusi.docx
mayank272369
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
alphaagenciesindia
 
#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf
lakshmijewellery
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
aassecuritysystem
 

Similar to #include iostream #include fstream #include cstdlib #.pdf (20)

So here is the code from the previous assignment that we need to ext.pdf
So here is the code from the previous assignment that we need to ext.pdfSo here is the code from the previous assignment that we need to ext.pdf
So here is the code from the previous assignment that we need to ext.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docx
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
Help with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfHelp with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdf
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad baloch
 
PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf
 PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf
PersonData.h#ifndef PersonData_h #define PersonData_h#inclu.pdf
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx
 
#include iostream#include string#include fstreamusi.docx
#include iostream#include string#include fstreamusi.docx#include iostream#include string#include fstreamusi.docx
#include iostream#include string#include fstreamusi.docx
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf#include iostream #includestdlib.h using namespace std;str.pdf
#include iostream #includestdlib.h using namespace std;str.pdf
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 

More from annesmkt

In the automata theory, a nondeterministic finite.pdf
                     In the automata theory, a nondeterministic finite.pdf                     In the automata theory, a nondeterministic finite.pdf
In the automata theory, a nondeterministic finite.pdf
annesmkt
 
Formaldehyde is an organic compound with the form.pdf
                     Formaldehyde is an organic compound with the form.pdf                     Formaldehyde is an organic compound with the form.pdf
Formaldehyde is an organic compound with the form.pdf
annesmkt
 
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdfThe Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
annesmkt
 
The employer must estimate the total cost of the benefits promised a.pdf
The employer must estimate the total cost of the benefits promised a.pdfThe employer must estimate the total cost of the benefits promised a.pdf
The employer must estimate the total cost of the benefits promised a.pdf
annesmkt
 
public class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdfpublic class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdf
annesmkt
 
Prior to 2010, the United States banned HIV-positive individuals fro.pdf
Prior to 2010, the United States banned HIV-positive individuals fro.pdfPrior to 2010, the United States banned HIV-positive individuals fro.pdf
Prior to 2010, the United States banned HIV-positive individuals fro.pdf
annesmkt
 
import java.util.Scanner;public class Bottle {    private stat.pdf
import java.util.Scanner;public class Bottle {    private stat.pdfimport java.util.Scanner;public class Bottle {    private stat.pdf
import java.util.Scanner;public class Bottle {    private stat.pdf
annesmkt
 
In previous year, 2 birthdays were on tuesday but this year theres.pdf
In previous year, 2 birthdays were on tuesday but this year theres.pdfIn previous year, 2 birthdays were on tuesday but this year theres.pdf
In previous year, 2 birthdays were on tuesday but this year theres.pdf
annesmkt
 

More from annesmkt (20)

six lone pairs (each F has three lone pairs) nit.pdf
                     six lone pairs (each F has three lone pairs)  nit.pdf                     six lone pairs (each F has three lone pairs)  nit.pdf
six lone pairs (each F has three lone pairs) nit.pdf
 
silver cyanide i think coz HCN will be formed whi.pdf
                     silver cyanide i think coz HCN will be formed whi.pdf                     silver cyanide i think coz HCN will be formed whi.pdf
silver cyanide i think coz HCN will be formed whi.pdf
 
Rh (Rhodium) is the answer. Solution .pdf
                     Rh (Rhodium) is the answer.  Solution        .pdf                     Rh (Rhodium) is the answer.  Solution        .pdf
Rh (Rhodium) is the answer. Solution .pdf
 
please rate me.... .pdf
                     please rate me....                               .pdf                     please rate me....                               .pdf
please rate me.... .pdf
 
No. The Mo is in +3 oxidation state and thus its .pdf
                     No. The Mo is in +3 oxidation state and thus its .pdf                     No. The Mo is in +3 oxidation state and thus its .pdf
No. The Mo is in +3 oxidation state and thus its .pdf
 
In the automata theory, a nondeterministic finite.pdf
                     In the automata theory, a nondeterministic finite.pdf                     In the automata theory, a nondeterministic finite.pdf
In the automata theory, a nondeterministic finite.pdf
 
Formaldehyde is an organic compound with the form.pdf
                     Formaldehyde is an organic compound with the form.pdf                     Formaldehyde is an organic compound with the form.pdf
Formaldehyde is an organic compound with the form.pdf
 
d. the molecular orbital diagram for O2. .pdf
                     d. the molecular orbital diagram for O2.         .pdf                     d. the molecular orbital diagram for O2.         .pdf
d. the molecular orbital diagram for O2. .pdf
 
Water molecules do not participate in acid base neutralization react.pdf
Water molecules do not participate in acid base neutralization react.pdfWater molecules do not participate in acid base neutralization react.pdf
Water molecules do not participate in acid base neutralization react.pdf
 
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdfThe Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
 
The employer must estimate the total cost of the benefits promised a.pdf
The employer must estimate the total cost of the benefits promised a.pdfThe employer must estimate the total cost of the benefits promised a.pdf
The employer must estimate the total cost of the benefits promised a.pdf
 
slopeSolutionslope.pdf
slopeSolutionslope.pdfslopeSolutionslope.pdf
slopeSolutionslope.pdf
 
Plz send the clear image to me...so that i can solve it..Solutio.pdf
Plz send the clear image to me...so that i can solve it..Solutio.pdfPlz send the clear image to me...so that i can solve it..Solutio.pdf
Plz send the clear image to me...so that i can solve it..Solutio.pdf
 
public class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdfpublic class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdf
 
Prior to 2010, the United States banned HIV-positive individuals fro.pdf
Prior to 2010, the United States banned HIV-positive individuals fro.pdfPrior to 2010, the United States banned HIV-positive individuals fro.pdf
Prior to 2010, the United States banned HIV-positive individuals fro.pdf
 
option (e) is correct.the feature that distinguishes fluorescence .pdf
option (e) is correct.the feature that distinguishes fluorescence .pdfoption (e) is correct.the feature that distinguishes fluorescence .pdf
option (e) is correct.the feature that distinguishes fluorescence .pdf
 
NADP is the source of electrons for the light reactionsThe electro.pdf
NADP is the source of electrons for the light reactionsThe electro.pdfNADP is the source of electrons for the light reactionsThe electro.pdf
NADP is the source of electrons for the light reactionsThe electro.pdf
 
import java.util.Scanner;public class Bottle {    private stat.pdf
import java.util.Scanner;public class Bottle {    private stat.pdfimport java.util.Scanner;public class Bottle {    private stat.pdf
import java.util.Scanner;public class Bottle {    private stat.pdf
 
In previous year, 2 birthdays were on tuesday but this year theres.pdf
In previous year, 2 birthdays were on tuesday but this year theres.pdfIn previous year, 2 birthdays were on tuesday but this year theres.pdf
In previous year, 2 birthdays were on tuesday but this year theres.pdf
 
Homology is defined as structures which are derived from a common an.pdf
Homology is defined as structures which are derived from a common an.pdfHomology is defined as structures which are derived from a common an.pdf
Homology is defined as structures which are derived from a common an.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

#include iostream #include fstream #include cstdlib #.pdf

  • 1. #include #include #include #include using namespace std; struct city { string name; // name of the city city *next; // pointer to the next city int numberMessages; // how many messages passed through this city string message; // message we are sending accross }; city* addCity(city *head, city *previous, string cityName ); city* deleteCity(city *head, string cityName); city* deleteEntireNetwork(city *head); city* searchNetwork(city *head, string cityName); city* loadDefaultSetup(city *head); void transmitMsg(city *head, string receiver, string filename); void printPath(city *head); void displayMenu(); city* handleUserInput(city *head); /* Do NOT modify main function */ int main(int argc, char* argv[]) { // pointer to the head of our network of cities city *head = NULL; head = handleUserInput(head); printPath(head); head = deleteEntireNetwork(head); if (head == NULL) cout << "Path cleaned" << endl; else printPath(head); cout << "Goodbye!" << endl;
  • 2. return 0; } /* * Purpose: handle the interaction with the user * Do NOT modify */ city* handleUserInput(city *head) { bool quit = false; string s_input; int input; // loop until the user quits while (!quit) { displayMenu(); // read in input, assuming a number comes in getline(cin, s_input); input = stoi(s_input); switch (input) { // print all nodes case 1: //rebuild network head = loadDefaultSetup(head); printPath(head); break; case 2: // print path printPath(head); break; case 3: //message is read in from file { string cityReceiver; cout << "Enter name of the city to receive the message: " << endl; getline(cin, cityReceiver); cout << "Enter the message to send: " << endl; string message; getline(cin, message);
  • 3. transmitMsg(head, cityReceiver, message); } break; case 4: { string newCityName; string prevCityName; cout << "Enter a new city name: " << endl; getline(cin, newCityName); cout << "Enter the previous city name (or First): " << endl; getline(cin, prevCityName); // find the node containing prevCity city *tmp = NULL; if(prevCityName !="First") tmp = searchNetwork(head, prevCityName); // add the new node head = addCity(head, tmp, newCityName); printPath(head); } break; case 5: // delete city { string city; cout << "Enter a city name: " << endl; getline(cin, city); head = deleteCity(head, city); printPath(head); } break; case 6: // delete network head = deleteEntireNetwork(head); break; case 7: // quit quit = true; cout << "Quitting... cleaning up path: " << endl; break;
  • 4. default: // invalid input cout << "Invalid Input" << endl; break; } } return head; } /* * Purpose: Add a new city to the network * between the city *previous and the city that follows it in the network. */ city* addCity(city *head, city *previous, string cityName ) { city* add = new city; //to distinguish and create the new node, i named it add add->name = cityName; city* tmp = new city; tmp=head; if(previous == NULL){ add->next = head; head = add; } while(tmp != NULL && previous != tmp){ tmp = tmp->next; } if(tmp == NULL){ if(head == NULL){ // if the head is null then it creates a node for the head head = add; add->next = NULL; } return head; } else{ add->next = tmp->next; //the pointer for the new node will be the next of the prev node tmp->next = add; // the prev node will now point to the new node }
  • 5. return head; } /* * Purpose: Search the network for the specified city and return a pointer to that node */ city *searchNetwork(city *ptr, string cityName) { city *tmp = new city; tmp = ptr; while(tmp != NULL && tmp->name != cityName){ tmp = tmp->next; } return tmp; } /* * Purpose: deletes all cities in the network starting at the head city. */ city* deleteEntireNetwork(city *ptr) { city *tmp = new city; while(ptr->next != NULL){ tmp = ptr->next; cout << "deleting: " << ptr->name << endl; delete ptr; ptr = tmp; } //stops at the last node, so we need to delete it and make ptr = null cout << "deleting: " << ptr->name << endl; delete ptr; cout << "Deleted network" << endl; // return head, which should be NULL return ptr; } /* sends messages from file */ void transmitMsg(city *head, string receiver, string message) {
  • 6. city *sender = new city; sender = head; if(head == NULL) { cout << "Empty list" << endl; return; } else{ while(sender != NULL && sender->name != receiver){ sender->numberMessages = sender->numberMessages + 1; //adds to the amount of times the city has intercepted a message sender->message = message; //the message will be changed everytime for every city cout << sender->name << " [# messages passed: " << sender->numberMessages << "] received: " << sender->message << endl; sender = sender->next; // moves on to the next city } // when the destination city is found the same 3 lines of code above will run but will end in termination of the function sender->numberMessages = sender->numberMessages +1; sender->message = message; cout << sender->name << " [# messages passed: " << sender->numberMessages << "] received: " << sender->message << endl; } } /* * Purpose: delete the city in the network with the specified name. */ city* deleteCity(city *head, string cityName) { city *tmp = new city; //will traverse the list city *prev = new city; // will hold the previous city for tmp tmp = head; if(cityName == head->name){ tmp = head->next; delete head; head = tmp;
  • 7. return head; } while(tmp != NULL && tmp->name != cityName){ prev = tmp; tmp = tmp->next; } if(tmp == NULL){ cout << "City does not exist." << endl; } else{ prev->next = tmp->next; //changes the previous next pointer to skip the deleted node delete tmp; } return head; } /* * Purpose: prints the current list nicely */ void printPath(city *ptr) { cout << "== CURRENT PATH ==" << endl; if (ptr == NULL){ cout << "nothing in path" << endl; } else{ city *tmp = new city; //to traverse the linked list tmp = ptr; while(tmp !=NULL){ //ends the traversal at the end of the list cout << tmp->name << " -> "; tmp = tmp->next; } cout << "NULL"<next; addCity(head,tmp,"Denver"); tmp = tmp->next;
  • 8. addCity(head,tmp,"Dallas"); tmp = tmp->next; addCity(head,tmp,"Atlanta"); tmp = tmp->next; addCity(head,tmp,"New York"); return head; } /* * Purpose; displays a menu with options */ void displayMenu() { cout << "Select a numerical option:" << endl; cout << "======Main Menu=====" << endl; cout << "1. Build Network" << endl; cout << "2. Print Network Path" << endl; cout << "3. Transmit Message" << endl; cout << "4. Add City" << endl; cout << "5. Delete City" << endl; cout << "6. Clear Network" << endl; cout << "7. Quit" << endl; } Solution #include #include #include #include using namespace std; struct city { string name; // name of the city city *next; // pointer to the next city int numberMessages; // how many messages passed through this city
  • 9. string message; // message we are sending accross }; city* addCity(city *head, city *previous, string cityName ); city* deleteCity(city *head, string cityName); city* deleteEntireNetwork(city *head); city* searchNetwork(city *head, string cityName); city* loadDefaultSetup(city *head); void transmitMsg(city *head, string receiver, string filename); void printPath(city *head); void displayMenu(); city* handleUserInput(city *head); /* Do NOT modify main function */ int main(int argc, char* argv[]) { // pointer to the head of our network of cities city *head = NULL; head = handleUserInput(head); printPath(head); head = deleteEntireNetwork(head); if (head == NULL) cout << "Path cleaned" << endl; else printPath(head); cout << "Goodbye!" << endl; return 0; } /* * Purpose: handle the interaction with the user * Do NOT modify */ city* handleUserInput(city *head) { bool quit = false; string s_input; int input; // loop until the user quits
  • 10. while (!quit) { displayMenu(); // read in input, assuming a number comes in getline(cin, s_input); input = stoi(s_input); switch (input) { // print all nodes case 1: //rebuild network head = loadDefaultSetup(head); printPath(head); break; case 2: // print path printPath(head); break; case 3: //message is read in from file { string cityReceiver; cout << "Enter name of the city to receive the message: " << endl; getline(cin, cityReceiver); cout << "Enter the message to send: " << endl; string message; getline(cin, message); transmitMsg(head, cityReceiver, message); } break; case 4: { string newCityName; string prevCityName; cout << "Enter a new city name: " << endl; getline(cin, newCityName); cout << "Enter the previous city name (or First): " << endl; getline(cin, prevCityName); // find the node containing prevCity
  • 11. city *tmp = NULL; if(prevCityName !="First") tmp = searchNetwork(head, prevCityName); // add the new node head = addCity(head, tmp, newCityName); printPath(head); } break; case 5: // delete city { string city; cout << "Enter a city name: " << endl; getline(cin, city); head = deleteCity(head, city); printPath(head); } break; case 6: // delete network head = deleteEntireNetwork(head); break; case 7: // quit quit = true; cout << "Quitting... cleaning up path: " << endl; break; default: // invalid input cout << "Invalid Input" << endl; break; } } return head; } /* * Purpose: Add a new city to the network * between the city *previous and the city that follows it in the network. */ city* addCity(city *head, city *previous, string cityName )
  • 12. { city* add = new city; //to distinguish and create the new node, i named it add add->name = cityName; city* tmp = new city; tmp=head; if(previous == NULL){ add->next = head; head = add; } while(tmp != NULL && previous != tmp){ tmp = tmp->next; } if(tmp == NULL){ if(head == NULL){ // if the head is null then it creates a node for the head head = add; add->next = NULL; } return head; } else{ add->next = tmp->next; //the pointer for the new node will be the next of the prev node tmp->next = add; // the prev node will now point to the new node } return head; } /* * Purpose: Search the network for the specified city and return a pointer to that node */ city *searchNetwork(city *ptr, string cityName) { city *tmp = new city; tmp = ptr; while(tmp != NULL && tmp->name != cityName){ tmp = tmp->next;
  • 13. } return tmp; } /* * Purpose: deletes all cities in the network starting at the head city. */ city* deleteEntireNetwork(city *ptr) { city *tmp = new city; while(ptr->next != NULL){ tmp = ptr->next; cout << "deleting: " << ptr->name << endl; delete ptr; ptr = tmp; } //stops at the last node, so we need to delete it and make ptr = null cout << "deleting: " << ptr->name << endl; delete ptr; cout << "Deleted network" << endl; // return head, which should be NULL return ptr; } /* sends messages from file */ void transmitMsg(city *head, string receiver, string message) { city *sender = new city; sender = head; if(head == NULL) { cout << "Empty list" << endl; return; } else{ while(sender != NULL && sender->name != receiver){ sender->numberMessages = sender->numberMessages + 1; //adds to the amount of times the city has intercepted a message sender->message = message; //the message will be changed everytime for every city
  • 14. cout << sender->name << " [# messages passed: " << sender->numberMessages << "] received: " << sender->message << endl; sender = sender->next; // moves on to the next city } // when the destination city is found the same 3 lines of code above will run but will end in termination of the function sender->numberMessages = sender->numberMessages +1; sender->message = message; cout << sender->name << " [# messages passed: " << sender->numberMessages << "] received: " << sender->message << endl; } } /* * Purpose: delete the city in the network with the specified name. */ city* deleteCity(city *head, string cityName) { city *tmp = new city; //will traverse the list city *prev = new city; // will hold the previous city for tmp tmp = head; if(cityName == head->name){ tmp = head->next; delete head; head = tmp; return head; } while(tmp != NULL && tmp->name != cityName){ prev = tmp; tmp = tmp->next; } if(tmp == NULL){ cout << "City does not exist." << endl; } else{ prev->next = tmp->next; //changes the previous next pointer to skip the deleted node delete tmp;
  • 15. } return head; } /* * Purpose: prints the current list nicely */ void printPath(city *ptr) { cout << "== CURRENT PATH ==" << endl; if (ptr == NULL){ cout << "nothing in path" << endl; } else{ city *tmp = new city; //to traverse the linked list tmp = ptr; while(tmp !=NULL){ //ends the traversal at the end of the list cout << tmp->name << " -> "; tmp = tmp->next; } cout << "NULL"<next; addCity(head,tmp,"Denver"); tmp = tmp->next; addCity(head,tmp,"Dallas"); tmp = tmp->next; addCity(head,tmp,"Atlanta"); tmp = tmp->next; addCity(head,tmp,"New York"); return head; } /* * Purpose; displays a menu with options */ void displayMenu() {
  • 16. cout << "Select a numerical option:" << endl; cout << "======Main Menu=====" << endl; cout << "1. Build Network" << endl; cout << "2. Print Network Path" << endl; cout << "3. Transmit Message" << endl; cout << "4. Add City" << endl; cout << "5. Delete City" << endl; cout << "6. Clear Network" << endl; cout << "7. Quit" << endl; }