Facebook
From Mateusz, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 278
  1. #include <stdexcept>
  2. #include <iostream>
  3. #include <list>
  4.  
  5. class TrainComposition
  6. {
  7. private:
  8.         std::list<int> trains;
  9. public:
  10.  
  11.         void attachWagonFromLeft(int wagonId)
  12.         {
  13.                 trains.push_front(wagonId);
  14.         }
  15.        
  16.         void attachWagonFromRight(int wagonId)
  17.         {
  18.                 trains.push_back(wagonId);
  19.         }
  20.  
  21.         int detachWagonFromLeft()
  22.         {
  23.                 auto temp = trains.front();
  24.                 trains.pop_front();
  25.                 return temp;
  26.         }
  27.  
  28.         int detachWagonFromRight()
  29.         {
  30.                 auto temp = trains.back();
  31.                 trains.pop_back();
  32.                 return temp;
  33.         }
  34. };
  35.  
  36. #ifndef RunTests
  37. int main()
  38. {
  39.         TrainComposition tree;
  40.         tree.attachWagonFromLeft(7);
  41.         tree.attachWagonFromLeft(13);
  42.         std::cout << tree.detachWagonFromRight() << "\n"; // 7
  43.         std::cout << tree.detachWagonFromLeft(); // 13
  44.         return EXIT_SUCCESS;
  45. }
  46. #endif