C++ This is what i have as a solution. Can you check If this makes sense according to the question if yes is there any logic errors. Are the functions doing what is said in the question. A. ~Train() { Car* current = tHead; for (int i = 0; current != NULL; i++) { Car* next = current->nextCar; delete current; current = next; } } B. void removeFirstCarOfType(const std::string& name) { if (tHead == NULL) { return; } Car* temp = tHead; if (temp->getName() == name) { tHead = temp->nextCar; free(temp); return; } int pos = 0; for (int i = 0; temp != NULL; i++) { if (temp->getName() == name) { pos = i; break; } temp = temp->nextCar; } if (temp == NULL || temp->nextCar == NULL) { return; } temp = tHead; for (int i = 0; temp != NULL && i < pos - 1; i++) { temp = temp->nextCar; } Car* nextNode = temp->nextCar->nextCar; free(temp->nextCar); temp->nextCar = nextNode; }
C++
This is what i have as a solution. Can you check If this makes sense according to the question if yes is there any logic errors. Are the functions doing what is said in the question.
A.
~Train() {
Car* current = tHead;
for (int i = 0; current != NULL; i++) {
Car* next = current->nextCar;
delete current;
current = next;
}
}
B.
void removeFirstCarOfType(const std::string& name) {
if (tHead == NULL) {
return;
}
Car* temp = tHead;
if (temp->getName() == name) {
tHead = temp->nextCar;
free(temp);
return;
}
int pos = 0;
for (int i = 0; temp != NULL; i++) {
if (temp->getName() == name) {
pos = i;
break;
}
temp = temp->nextCar;
}
if (temp == NULL || temp->nextCar == NULL) {
return;
}
temp = tHead;
for (int i = 0; temp != NULL && i < pos - 1; i++) {
temp = temp->nextCar;
}
Car* nextNode = temp->nextCar->nextCar;
free(temp->nextCar);
temp->nextCar = nextNode;
}
![1) Within the solution box, implement the following:
A. The Train class's destructor, the destructor should properly destroy the linked list of train cars. You may
assume the other functions are implemented correctly.
B. The Train class's function: removeFirstCarofType. This function removes the first car from the train car
list that has a string name that matches the provided string. Do not use any other Train class functions.
Your solution for the above functions should go in this box, which should be considered part of train.cpp:](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F672bf286-8abe-4b07-9ca1-0d5b2612956c%2F27ec20b3-fed3-4af5-aea5-0267f0ab08dc%2Fa4gcvrv_processed.png&w=3840&q=75)
![The following class definitions and function definitions should be used to help you
train.h:
car.h:
#include "car.h"
using namespace std;
#ifndef TRAIN_H
#define TRAIN_H
class Train {
private:
Car* tHead;
Car* tTail;
public:
Train();
-Train();
void addCarBack (Car *);
void deleteFirstCar();
void removeFirstCarofType(const string &);
#ifndef CAR_H
#define CAR_H
using namespace std;
class Car
{
private:
string name;
double maxSpeed;
public:
Car* nextCar;
// Display list car by car
void display() const;
};
#endif
public:
Car();
Car(string);
double getMaxSpeed() const;
string getName() const;
void setName (const string &);
};
#endif
Part of train.cpp:
// addCarBack logic:
if (trainCarsHead == 0) {
trainCarsHead - toAdd;
trainCarsTail = toAdd;
}
else {
trainCarsTail->nextCar = toAdd;
trainCarsTail = toAdd;
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F672bf286-8abe-4b07-9ca1-0d5b2612956c%2F27ec20b3-fed3-4af5-aea5-0267f0ab08dc%2Fiho4rq9_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)