Hi,you covered mostly things.there are issue to point and link poi.pdf
1. Hi,
you covered mostly things.there are issue to point and link pointer.I will try my best to solve
your issue.
I have added below functions which are wrong or missing.
city* addCity(city *head, city *previous, string cityName )
{
if (head == NULL)
{
head = new city;
head->name = cityName;
return head;
}
city *newNode = new city;
newNode->name = cityName;
if (previous == NULL)
{
newNode->next = head;
head = newNode;
return head;
}
newNode->next = previous->next;
previous->next = newNode;
cout << "prev: " << previous->name << " new: " << cityName << endl;
return head;
}
newNode->name = cityName;
city *current = new city;
current = searchNetwork(head, previous->name);
2. newNode->next = current->next;
previous->next = newNode;
cout << "prev: " << previous->name << " new: " << cityName << endl;
city *searchNetwork(city *ptr, string cityName)
{
city *current = ptr;
while (current->name != cityName)
{
if (current->name != cityName && current->next == NULL)
return NULL;
current = current->next;
}
return current;
}
city* deleteCity(city *head, string cityName)
{
city *current = head;
cout << "Checkpoint 1" << endl;
while (current->next->name != cityName)
{
if (current->next == NULL)
{
cout << "City does not exist." << endl;
return head;
}
current = current->next;
}
3. cout << "Checkpoint 2" << endl;
current->next = current->next->next;
return head;
}
void printPath(city *ptr)
{
cout << "== CURRENT PATH ==" << endl;
// If the head is NULL
if (ptr == NULL)
cout << "nothing in path" << endl;
else
{
city *temp2 = ptr;
while( temp2!=NULL )
{
cout << temp2->name << " -> ";// show the data in the linked list
temp2 = temp2->next; // tranfer the address of 'temp->next' to 'temp'
}
cout << "NULL" << endl;
}
cout << "===" << endl;
}
city* loadDefaultSetup(city *head)
{
head = deleteEntireNetwork(head);
head = addCity(head,NULL,"Los Angeles");
city *two = new city;
city *three = new city;
city *four = new city;
city *five = new city;
4. city *six = new city;
two->name = "Phoenix";
three->name = "Denver";
four->name = "Dallas";
five->name = "Atlanta";
six->name = "New York";
head->next = two;
two->next = three;
three->next = four;
four->next = five;
five->next = six;
six->next = NULL;
return head;
}
Solution
Hi,
you covered mostly things.there are issue to point and link pointer.I will try my best to solve
your issue.
I have added below functions which are wrong or missing.
city* addCity(city *head, city *previous, string cityName )
{
if (head == NULL)
{
head = new city;
head->name = cityName;
return head;
}
5. city *newNode = new city;
newNode->name = cityName;
if (previous == NULL)
{
newNode->next = head;
head = newNode;
return head;
}
newNode->next = previous->next;
previous->next = newNode;
cout << "prev: " << previous->name << " new: " << cityName << endl;
return head;
}
newNode->name = cityName;
city *current = new city;
current = searchNetwork(head, previous->name);
newNode->next = current->next;
previous->next = newNode;
cout << "prev: " << previous->name << " new: " << cityName << endl;
city *searchNetwork(city *ptr, string cityName)
{
city *current = ptr;
while (current->name != cityName)
{
if (current->name != cityName && current->next == NULL)
return NULL;
current = current->next;
}
6. return current;
}
city* deleteCity(city *head, string cityName)
{
city *current = head;
cout << "Checkpoint 1" << endl;
while (current->next->name != cityName)
{
if (current->next == NULL)
{
cout << "City does not exist." << endl;
return head;
}
current = current->next;
}
cout << "Checkpoint 2" << endl;
current->next = current->next->next;
return head;
}
void printPath(city *ptr)
{
cout << "== CURRENT PATH ==" << endl;
// If the head is NULL
if (ptr == NULL)
cout << "nothing in path" << endl;
else
{
city *temp2 = ptr;
7. while( temp2!=NULL )
{
cout << temp2->name << " -> ";// show the data in the linked list
temp2 = temp2->next; // tranfer the address of 'temp->next' to 'temp'
}
cout << "NULL" << endl;
}
cout << "===" << endl;
}
city* loadDefaultSetup(city *head)
{
head = deleteEntireNetwork(head);
head = addCity(head,NULL,"Los Angeles");
city *two = new city;
city *three = new city;
city *four = new city;
city *five = new city;
city *six = new city;
two->name = "Phoenix";
three->name = "Denver";
four->name = "Dallas";
five->name = "Atlanta";
six->name = "New York";
head->next = two;
two->next = three;
three->next = four;
four->next = five;
five->next = six;
six->next = NULL;