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 void PrintTrain(Locomotive train) { Write("\n"); RailCar railCar = train.getFirstRailCar(); Write("L"); while (railCar != null) { Write($"-{railCar.toString()}"); railCar = railCar.getNext(); } }
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); }
public void attachRailCar(RailCar newCar) { if (first == null) { first = newCar; } else { RailCar append = first; bool insert = false; if (string.Compare(newCar.toString(), first.toString()) == -1) { newCar.setNext(first); first = newCar; } else { while (append != null && !insert) { if (append.getNext() == null) { insert = true; append.setNext(newCar); } else if (string.Compare(newCar.toString(), append.getNext().toString()) == -1) { insert = true; newCar.setNext(append.getNext()); append.setNext(newCar); } append = append.getNext(); } } } }