Пример #1
0
        static void PrintTrain(Locomotive train)
        {
            Write("\n");
            RailCar railCar = train.getFirstRailCar();

            Write("L");
            while (railCar != null)
            {
                Write($"-{railCar.toString()}");
                railCar = railCar.getNext();
            }
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }