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; }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter3: Using Methods, Classes, And Objects
Section: Chapter Questions
Problem 16RQ
icon
Related questions
Question
100%

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:
Transcribed Image Text: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:
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;
}
Transcribed Image Text: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; }
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT