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

C++ I need help with a function which is giving me a segmentation fault. Here it

ID: 3588850 • Letter: C

Question

C++ I need help with a function which is giving me a segmentation fault. Here it is. It is supposed to delete each city in a linked list. If you could let me know what in the function is problematic that would be great

city* deleteEntireNetwork(city *ptr)

{

city *tmp = new city;

while(ptr -> next != NULL){

tmp = ptr -> next;

cout << "deleting: " << ptr -> name << endl;

delete ptr;

ptr = tmp;

}

cout << "deleting: " << ptr -> name << endl;

delete ptr;

cout << "Deleted network" << endl;

ptr = NULL;

return ptr;

}

Explanation / Answer

city* deleteEntireNetwork(city *ptr)
{
while(ptr){
city *tmp = ptr->next;
cout << "deleting: " << ptr->name << endl;
delete ptr;
ptr = tmp;
}
cout << "Deleted network" << endl;
return ptr;
}

Basically you were assing null after deleting.

If you still face issue please provide your full code so that I can simulate issue and resolve it.