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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education