-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain-composition.cpp
More file actions
64 lines (58 loc) · 1.43 KB
/
train-composition.cpp
File metadata and controls
64 lines (58 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @file train-composition.cpp
* @author nirmeet baweja
* @brief A TrainComposition is built by attaching and detaching wagons from
* he left and the right sides, efficiently with respect to time used.
*
* For example, if we start by attaching wagon 7 from the left followed by
* attaching wagon 13, again from the left, we get a composition of two wagons
* (13 and 7 from left to right). Now the first wagon that can be detached from
* the right is 7 and the first that can be detached from the left is 13.
*
* Implement a TrainComposition that models this problem.
*
* @version 0.1
* @date 2022-02-14
*
* @copyright Copyright (c) 2022
*
*/
#include <stdexcept>
#include <iostream>
#include <deque>
class TrainComposition
{
private:
std::deque<int> wagons;
public:
void attachWagonFromLeft(int wagonId)
{
this->wagons.push_front(wagonId);
}
void attachWagonFromRight(int wagonId)
{
this->wagons.push_back(wagonId);
}
int detachWagonFromLeft()
{
int wagon = wagons[0];
this->wagons.pop_front();
return wagon;
}
int detachWagonFromRight()
{
int wagon = this->wagons[this->wagons.size() - 1];
this->wagons.pop_back();
return wagon;
}
};
#ifndef RunTests
int main()
{
TrainComposition train;
train.attachWagonFromLeft(7);
train.attachWagonFromLeft(13);
std::cout << train.detachWagonFromRight() << "\n"; // 7
std::cout << train.detachWagonFromLeft(); // 13
}
#endif