Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need a function that adds a struct to a linked list Add City: This option allows

ID: 3873675 • Letter: N

Question

 Need a function that adds a struct to a linked list  Add City: This option allows the user to add a new city to the network. If the user selects this option, then they should be prompted for the name of the city and the city that the new city should follow in the network. For example, if the user wants to add Tucson after Phoenix in the network, then the first four cities in the network would be: Los Angeles -> Phoenix -> Tucson -> Denver... If the user wants to add a new city to the head of the network, e.g. replace Los Angeles as the starting city, then they should type First when prompted for the previous city and your code should handle this special case.  /* 
 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 }; 
   * Purpose: Add a new city to the network * between the city *previous and the city that follows it in the network. * Prints: `prev: <city name> new: <city name>` when a city is added, * prints _nothing_ if the city is being added to the _first_ position in * the list. * @param head pointer to start of the list * @param previous name of the city that comes before the new city * @param cityName name of the new city * @return pointer to first node in list */ city* addCity(city *head, city *previous, string cityName ) {     cout << "prev: " << previous->name << " new: " << cityName << endl;     return head; } 

Explanation / Answer

Please find my implementation.

/*
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
};


* Purpose: Add a new city to the network
* between the city *previous and the city that follows it in the network.
* Prints: `prev: <city name> new: <city name>` when a city is added,
* prints _nothing_ if the city is being added to the _first_ position in
* the list.
* @param head pointer to start of the list
* @param previous name of the city that comes before the new city
* @param cityName name of the new city
* @return pointer to first node in list
*/
city* addCity(city *head, city *previous, string cityName )
{
  
city *newCity = new city;
newCity->name = cityName;

if(head->name.compare(previous->name) == 0){

city->next = head;
head = newCity;
}else{
city *temp = head;
while( temp != NULL && (temp->name.compare(previous->name) != 0)) {
temp = temp->next;
}

if(temp != NULL) {
cout << "prev: " << previous->name << " new: " << cityName << endl;
newCity->next = temp->next;
temp->next = newCity;
}
}

return head;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote