Question
Topics: Linked Lists Use the same format for problem and function headings as assignment 1. Problem 6.1 (use the data in cardata6.txt as shown below)
Topics:
Linked Lists
Use the same format for problem and function headings as assignment 1.
Problem 6.1 (use the data in cardata6.txt as shown below)
Copy the solution from problem 5.2.
You will change the implementation of the StringOfCars class, but keep the public interface.
This implementation will use a linked list, rather than an array or vector to hold the cars.
Keep all the public function prototypes in the StringOfCars.
Discard all the private data and the implementation of the functions; you will rebuild all of
these.
Do not change anything outside the StringOfCars class
.
Build a new class called Node:
You will build the linked list using Node objects, linked together to make a list.
In the private data of the Node class put two pointers:
One with type Node * and name next, which will point to the next node in the linked list,
The second with type Car* and name data, which will point to the Car data associated with this
node.
Also in the private area create a default constructor that sets the next and data pointers to
zero. Because the constructor is private, only friends can use this class.
In the public area of the Node class, make StringOfCars a friend class.
The order of the following three things is important:
1. Declare the StringOfCars class with: class StringOfCars;
2. The Node class
3. The StringOfCars class
This is needed because the Node class uses the StringOfCars and the StringOfCars class uses the
Node class.
In the StringOfCars class implementation:
Replace the private data with two pointers of type Node *, and nothing else. Name these two
pointers
head
and
tail
.
Change the StringOfCars default constructor to set the head and tail pointers to zero.
Rebuild the push function, with the same function heading.
Declare a local pointer variable of type Car * named currentCarPtr.
Declare a local pointer variable of type Node * named currentNodePtr.
Use
new
to get space in the heap for a Node and save the address of the space in the heap in
currentNodePtr
Use new get space in the heap for a new Car that is a copy of the car parameter of the push
function and save the address of the space in the heap in currentCarPtr
Set the data pointer in this new Node object to point to the new Car object.
If the head pointer is zero
set the head and the tail pointer to the value of currentNodePtr
else
set the next pointer of the Node object pointed to by the tail pointer to the value
of currentNodePtr
set the next pointer to the value of the currentNodePtr
Do not write a pop function
.
Rebuild the output function, with the same function heading.
Declare a local pointer variable of type Node * named currentNodePtr - it will point to the Node
you are currently working on.
if the head pointer is zero
print: NO cars
else
set the currentNodePointer to the value of the head pointer
while the currentNodePointer is not zero
print the Car pointed to by the currentNodePointer
set the currentNodePtr to the next pointer in the Node pointed to by
the currentNodePtr, which now makes the next Node the current Node
Rebuild the copy construtor.
Declare a local pointer variable of type Node * named currentNodePtr - it will point to the Node
in the oldStringOfCars that you are currently working on.
Set the head and tail pointers in the StringOfCars being constructed to zero.
If the oldStringOfCars head pointer is not zero:
loop while the currentNodePointer is not zero,
push the Car pointed to by the data pointer in the current Node, which is pointed to by the
currentNodePointer.
set the currentNodePtr to the next pointer in the currentNodePtr so we now make the next
Node the current Node
Use the data in cardata6.txt to test your program. The data is shown below:
cardata6.txt
Car car1 SP 819487 maintenance false NONE
Car car2 NP 46877 business true Portland
Car car3 NS 157 tender true Saint Louis
FreightCar car4 PVT 123457 tank false Fort Worth
FreightCar car5 MP 98767 box true Saint Louis
FreightCar car6 SP 567897 flat true New York
FreightCar car7 NSF 7877 hopper true Texarkana
PassengerCar car8 KCS 7897 chair true Kansas City
PassengerCar car9 ATSF 147 sleeper true Tucson
PassengerCar car10 B&O 747 combine false Winslow
HERE'S CODE FOR 5.2
#include
#include
#include
#include
//Use namespace
using namespace std;
enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger };
const string KIND_ARRAY[] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight", "chair", "sleeper", "otherPassenger" };
//class car
class Car
{
//declare variables
protected:
string reportingMark;
int carNumber;
Kind kind;
bool loaded;
public:
string destination;
//declare member functions
void setUp(string rMark, int carNum, string carKind, bool isLoaded, string dest);
virtual void setKind(const string & tempKind);
void output();
Car& operator=(const Car & carB);
friend bool operator==(const Car &carA, const Car &carB);
//constructor
Car()
{
setUp("", 0, "other", false, "NONE");
}
//parametrized constructor
Car(const Car &oldCar)
{
setUp(oldCar.reportingMark, oldCar.carNumber, KIND_ARRAY[oldCar.kind], oldCar.loaded, oldCar.destination);
kind = oldCar.kind;
}
Car(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination)
{
setUp(reportingMark, carNumber, kind, loaded, destination);
}
int getCarNum() {
return carNumber;
}
string getRepMark() {
return reportingMark;
}
//destructor
virtual ~Car() {};
};
class FreightCar : public Car
{
public:
FreightCar()
{
setUp("", 0, "other", false, "NONE");
}
FreightCar(const FreightCar &oldFreight)
{
setUp(oldFreight.reportingMark, oldFreight.carNumber, KIND_ARRAY[oldFreight.kind], oldFreight.loaded, oldFreight.destination);
}
FreightCar(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination)
{
setUp(reportingMark, carNumber, kind, loaded, destination);
}
void setKind(const string & stringKind);
};
class PassengerCar : public Car
{
public:
PassengerCar()
{
setUp("", 0, "other", false, "NONE");
}
PassengerCar(const PassengerCar &oldPassenger)
{
setUp(oldPassenger.reportingMark, oldPassenger.carNumber, KIND_ARRAY[oldPassenger.kind], oldPassenger.loaded, oldPassenger.destination);
}
PassengerCar(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination)
{
setUp(reportingMark, carNumber, kind, loaded, destination);
}
void setKind(const string & stringKind);
};
//StringOfCars declaration for the Node class
class StringOfCars;
//class Node to connectnode data and link
class Node
{
//declare data members
private:
Node * next;
Car * data;
//define constructor to initialize the variables
Node()
{
next = nullptr;
data = nullptr;
}
public:
//Make StringOfCars as a friend class of Node
friend class StringOfCars;
};
class StringOfCars
{
private:
Node * head;
Node * tail;
public:
StringOfCars()
{
head = nullptr;
tail = nullptr;
}
StringOfCars(const StringOfCars & oldStringOfCars)
{
Node * currentNodePtr = oldStringOfCars.head;
head = nullptr;
tail = nullptr;
if (oldStringOfCars.head != nullptr)
{
while (currentNodePtr != nullptr)
{
push(*currentNodePtr->data);
currentNodePtr = (*currentNodePtr).next;
}
}
}
~StringOfCars() {}
void push(const Car & tempCar);
Car* search(int carNum);
void addCar();
void output();
};
//Function Prototypes
void input(StringOfCars & carArray);
void buildCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray);
void buildFreightCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray);
void buildPassengerCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray);
//main function
int main()
{
//create object for StringOfCars
StringOfCars stringOfCars1;
input(stringOfCars1);
StringOfCars stringOfCars2(stringOfCars1);
stringOfCars2.output();
stringOfCars1.addCar();
//Message to search the car
cout << "Enter the car number you'd like to search: ";
int carNumChoice;
cin >> carNumChoice;
cout << " Search results: ";
if (stringOfCars2.search(carNumChoice) != nullptr) {
(stringOfCars2.search(carNumChoice))->output();
}
else
{
cout << "No car found. ";
}
int closeWindow;
cout << " Enter an integer to close the output window: ";
cin >> closeWindow;
//Pause the system for a while
system("pause");//use only when using Visual Studio
return 0;
}
//output method to display the details of the file
void Car::output()
{
cout << "Reporting Mark: " << reportingMark << endl;
cout << "Car Number : " << carNumber << endl;
cout << "Kind : " << KIND_ARRAY[kind] << endl;
//Converts the loaded boolean into a string for output
string temp;
if (loaded == true)
temp = "true";
else
temp = "false";
cout << "Loaded : " << temp << endl;
cout << "Destination : " << destination << endl;
cout << endl;
}
//setUp function
void Car::setUp(string rMark, int carNum, string carKind, bool isLoaded, string dest)
{
reportingMark = rMark;
carNumber = carNum;
setKind(carKind);
loaded = isLoaded;
destination = dest;
}
//setKind value of car in this function
void Car::setKind(const string & stringKind)
{
if (stringKind == "business")
kind = business;
else if (stringKind == "maintenance")
kind = maintenance;
else kind = other;
}
//overloaded operator = and returns left hand side value
Car & Car::operator=(const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);
return *this;
}
//overload == operator and use for searching or equarting the values
bool operator==(const Car &carA, const Car &carB)
{
bool temp;
if (carA.reportingMark == carB.reportingMark && carA.carNumber == carB.carNumber)
temp = true;
else
temp = false;
return temp;
}
//To setKind value for class FreightCar
void FreightCar::setKind(const string & stringKind)
{
if (stringKind == "box")
kind = box;
else if (stringKind == "tank")
kind = tank;
else if (stringKind == "flat")
kind = flat;
else kind = otherFreight;
}
//Define the memberfunction setKind
void PassengerCar::setKind(const string & stringKind)
{
if (stringKind == "chair")
kind = chair;
else if (stringKind == "sleeper")
kind = sleeper;
else kind = otherPassenger;
}
//Addcar method to add cardetails
void StringOfCars::addCar() {
string type, order, rMark;
//Message to display on the output screen
cout << "Choose 1)Car 2)Passenger Car 3)Frieght Car 4)Exit ";
int choice;
//input choice
cin >> choice;
//start loop to add details in car
while (choice > 4) {
cout << "Sorry, please enter a valid input. ";
cin >> choice;
}
//to add car details
if (choice == 1) {
type = "Car";
}
//to add passenger car detils
else if (choice == 2) {
type = "PassengerCar";
}
//to add Freight Car details
else {
type = "FreightCar";
}
order = "car11";
//Input the details
cout << "Reporting Mark: ";
cin >> rMark;
cout << "Car number: ";
int carNum;
cin >> carNum;
cout << "Kind of car:: ";
string carKind;
cin >> carKind;
bool isLoaded;
isLoaded = true;
cout << "Destination: ";
string dest;
cin >> dest;
//call parametrized constructor by creating object
Car temp(rMark, carNum, carKind, isLoaded, dest);
push(temp);
}
//search whether car present or not in the list
Car* StringOfCars::search(int carNum) {
Node * pCurr;
if (head == nullptr) {
cout << "No cars ";
}
else {
pCurr = head;
while (pCurr != nullptr) {
if (carNum == (*pCurr->data).getCarNum()) {
return &(*pCurr->data);
}
pCurr = (*pCurr).next;
}
}
Car* temp = nullptr;
return temp;
}
//Display the car list
void StringOfCars::output()
{
Node * currentNodePtr;
if (head == nullptr)
{
cout << "No cars ";
}
else
{
currentNodePtr = head;
int currentCarNumber = 0;
while (currentNodePtr != NULL)
{
cout << "Car Number " << ++currentCarNumber << endl;
(*currentNodePtr->data).output();
currentNodePtr = (*currentNodePtr).next;
}
}
}
//Push details in the lost
void StringOfCars::push(const Car & tempCar)
{
//create pointer objects for classes
Car* currentCarPtr = new Car(tempCar);
Node* currentNodePtr = new Node;
//initialize data variable or node class
(*currentNodePtr).data = currentCarPtr;
if (head == nullptr)
{
head = currentNodePtr;
tail = currentNodePtr;
}
else
{
(*tail).next = currentNodePtr;
tail = currentNodePtr;
}
}
//Define input method to read the input from the file
void input(StringOfCars & carArray)
{
//declare data members
string type;
string order;
string rMark;
int carNum;
string carKind;
bool isLoaded;
string dest;
//create object of file
ifstream inputFile;
//open the file
inputFile.open("data.txt");
//if unable to open the file
if (!inputFile)
{
cerr << "Error while opening file." << endl;
exit(1);
}
//read the file
while (inputFile.peek() != EOF)
{
inputFile >> type;
inputFile >> order;
inputFile >> rMark;
inputFile >> carNum;
inputFile >> carKind;
//convert the value into true false for boolean
string temp;
inputFile >> temp;
if (temp == "true")
isLoaded = true;
else if (temp == "false")
isLoaded = false;
//Skips the white space
while (inputFile.peek() == ' ')
inputFile.get();
getline(inputFile, dest);
if (type == "Car")
buildCar(rMark, carNum, carKind, isLoaded, dest, carArray);
else if (type == "FreightCar")
buildFreightCar(rMark, carNum, carKind, isLoaded, dest, carArray);
else if (type == "PassengerCar")
buildPassengerCar(rMark, carNum, carKind, isLoaded, dest, carArray);
}
inputFile.close();
}
//build car object and push the result to the output
void buildCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray)
{
Car obj = Car(rMark, carNum, carKind, isLoaded, dest);
carArray.push(obj);
}
//buildFreightCar class object to add the data to the console
void buildFreightCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray)
{
FreightCar obj = FreightCar(rMark, carNum, carKind, isLoaded, dest);
carArray.push(obj);
}
//create object for passenger car and push the result to the output screen
void buildPassengerCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray)
{
PassengerCar obj = PassengerCar(rMark, carNum, carKind, isLoaded, dest);
carArray.push(obj);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started