Question & Answer: #include #include…..

#include<iostream>
#include <unordered_map>

using namespace std;

struct City{
char id;
int popularity;
string cityName;
//Only need to keep track of next city
City *nextCity;
};

//Primary storage of Cities
unordered_map<char, City> hashtable;

void printRoute(char src, char dest){
//Adding popularity to destination
hashtable[dest].popularity++;

char currCity=src;
City *city=&hashtable[currCity];

while(currCity!=dest){
printf(“%c->”,city->id);
city=hashtable[currCity].nextCity;
currCity=city->id;
}

printf(“%c n”,city->id);
}

void printPopularDestinations(){

for(int i=0;i<5;i++){
cout<<hashtable[i+65].id<<” “<<hashtable[i+65].cityName<<“: “<<hashtable[i+65].popularity<<“n”;
}
}

int main()
{
//Initialization
for(int i=0;i<5;i++){
City city;
city.id=i+65;
city.popularity=0;
hashtable[i+65]=city;
}

//read from console
printf(“Enter highways:n”);
for(int i=0;i<5;i++){
char src;
char dest;
cin>>src>>dest;
hashtable[src].nextCity=&hashtable[dest];
}
/*
hashtable[‘A’].nextCity=&hashtable[‘E’];
hashtable[‘E’].nextCity=&hashtable[‘C’];
hashtable[‘C’].nextCity=&hashtable[‘B’];
hashtable[‘B’].nextCity=&hashtable[‘D’];
hashtable[‘D’].nextCity=&hashtable[‘A’];
*/

printf(“Enter new names:n”);
for(int i=0;i<5;i++){
printf(“Enter name for %c:”,hashtable[i+65].id);
cin>>hashtable[i+65].cityName;
}

printRoute(‘A’,’D’);

bool exit = false;
printf(“nn”);
char option;
while (!exit){
printf(“C to enter another source destination pairn”);
printf(“P to see most popular destinations..n”);
printf(“E Exitn”);
cin>>option;

switch(option){
case ‘C’:
printf(“Enter Source Dest Pair:”);
char src;
char dest;
cin>>src>>dest;
printRoute(src,dest);
break;
case ‘P’:
printf(“Showing popular destinations:n”);
printPopularDestinations();
break;
case ‘E’:
exit=true;
break;
}

}

return 0;
}

Can you solve the code using vectors and pointers instead of hastables and unordered pair?

Expert Answer

 

Still stressed from student homework?
Get quality assistance from academic writers!