#include #include #include class TrainComposition { private: std::list trains; public: void attachWagonFromLeft(int wagonId) { trains.push_front(wagonId); } void attachWagonFromRight(int wagonId) { trains.push_back(wagonId); } int detachWagonFromLeft() { auto temp = trains.front(); trains.pop_front(); return temp; } int detachWagonFromRight() { auto temp = trains.back(); trains.pop_back(); return temp; } }; #ifndef RunTests int main() { TrainComposition tree; tree.attachWagonFromLeft(7); tree.attachWagonFromLeft(13); std::cout << tree.detachWagonFromRight() << "\n"; // 7 std::cout << tree.detachWagonFromLeft(); // 13 return EXIT_SUCCESS; } #endif