static void PrintTrain(Locomotive train) { Write("\n"); RailCar railCar = train.getFirstRailCar(); Write("L"); while (railCar != null) { Write($"-{railCar.toString()}"); railCar = railCar.getNext(); } }
static Locomotive RemoveUnprofitable(Locomotive L) { RailCar A = L.getFirstRailCar(); RailCar B = A.getNext(); if (B == null) { if (!A.isProfitable()) { L.setFirstRailCar(null); } } else { while (A != null && B != null) { if (!A.isProfitable()) { L.setFirstRailCar(B); A = B; B = B.getNext(); } else if (!B.isProfitable()) { B = B.getNext(); A.setNext(B); } else { A = B; B = B.getNext(); } } if (A != null) { if (!A.isProfitable()) { L.setFirstRailCar(null); } } } return(L); }
static Locomotive ReverseOrder(Locomotive L) { Locomotive reverse = new Locomotive(); reverse.setFirstRailCar(L.getFirstRailCar().clone()); RailCar first = reverse.getFirstRailCar(); first.setNext(null); RailCar car = L.getFirstRailCar(); RailCar newCar = L.getFirstRailCar(); while (car.getNext() != null) { newCar = car.getNext().clone(); newCar.setNext(first); reverse.setFirstRailCar(newCar); first = reverse.getFirstRailCar(); car = car.getNext(); } return(reverse); }