For this method can anyone help I just find a requirement fo
For this method can anyone help?
I just find a requirement for my assignment that if the cityname cannot find, then cout<<delCity<<\"not found\"<<endl;
but I tried to make this arguement in this method. I always got the output is not found even if the city name is in the linked list. And I realized my arguement was runned after the method deleted the city. So how can I write the code which can judge whether the city name is in the linked list or not?
void CommunicationNetwork::delCity(string delCity)
{
if (headCity->name == delCity)
{
City* tempCity = headCity;
head_city->next->prev = NULL;
head_city = head_city->next;
tempCity->next = NULL;
delete tempCity;
tempCity = NULL;
}
else
{
City *current_city = headCity;
while (current_city != NULL)
{
if (current_city->name == delCity)
{
current_city->prev->next = current_city->next;
current_city->next->prev = current_city->prev;
current_city->next = NULL;
current_city->next = NULL;
delete current_city;
current_city = NULL;
break;
}
current_city = current_city->next;
}
cout << \"done\" << endl;
}
Solution
void CommunicationNetwork::delCity(string delCity)
{
bool flag = true;
if (headCity->name == delCity)
{
City* tempCity = headCity;
head_city->next->prev = NULL;
head_city = head_city->next;
tempCity->next = NULL;
delete tempCity;
tempCity = NULL;
flag= false;
}
else
{
City *current_city = headCity;
while (current_city != NULL)
{
if (current_city->name == delCity)
{
current_city->prev->next = current_city->next;
current_city->next->prev = current_city->prev;
current_city->next = NULL;
current_city->next = NULL;
delete current_city;
current_city = NULL;
flag = false;
break;
}
current_city = current_city->next;
}
if(flag)
cout<<delCity<<\"not found\"<<endl;
cout << \"done\" << endl;
}
===========================================================
Can you run it now, It will tell you if delCity is not found

