1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <iostream>
#include <cstring>
#include "Train.h"
using namespace std;
struct TrainCar {
char * type[50];
float capacity;
int serial;
TrainCar *nextTrain = NULL;
};
TrainCar * createTrainCar(char *kind, int cargo, int serial) {
TrainCar *newTrain;
newTrain = new TrainCar;
strcpy(*newTrain->type, kind);
newTrain->capacity = cargo;
newTrain->serial = serial;
return newTrain;
}
void connectTrains(TrainCar *front, TrainCar *back) {
TrainCar *trainWalker;
TrainCar *tempCar;
trainWalker = front;
if (front == NULL) {
return;
}
while (trainWalker != NULL) {
tempCar = trainWalker;
trainWalker = trainWalker->nextTrain;
}
tempCar->nextTrain = back;
}
TrainCar * disconnectTrains(TrainCar train, int serial) {
TrainCar *trainWalker = NULL, *tempCar;
*trainWalker = train;
// walk along until the end, or unti you find the car
while ((trainWalker != NULL) && (trainWalker->serial != serial)) {
trainWalker = trainWalker->nextTrain;
}
if (trainWalker == NULL) {
return NULL;
}
tempCar = trainWalker->nextTrain;
trainWalker->nextTrain = NULL;
return tempCar;
}
void printTrain(TrainCar *train) {
TrainCar *trainToPrint = train;
while (trainToPrint != NULL) {
cout << "Type: " << trainToPrint->type << endl;
cout << "Capacity: " << trainToPrint->capacity << endl;
cout << "Serial Number: " << trainToPrint->serial << endl;
cout << "\n" << endl;
trainToPrint = trainToPrint->nextTrain;
}
}
int main() {
cout << "Test 1" << endl;
TrainCar *a = createTrainCar("engine", 0, 111);
TrainCar *b = createTrainCar("tanker", 8000, 115);
TrainCar *c = createTrainCar("tanker", 8000, 117);
TrainCar *d = createTrainCar("cargo", 6000, 214);
TrainCar *e = createTrainCar("cargo", 5000, 260);
TrainCar *f = createTrainCar("stock", 5000, 270);
cout << "Test 2" << endl;
connectTrains(a, b);
connectTrains(a, c);
connectTrains(a,d);
connectTrains(a,e);
connectTrains(a,f);
cout << "Test 3" << endl;
printTrain(a); // prints them all
cout << "Test 4" << endl;
return 0;
}
|