-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_min.cpp
89 lines (80 loc) · 1.71 KB
/
stack_min.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <vector>
#include <exception>
#include <initializer_list>
template<typename T>
class StackMin
{
std::vector<T> m_minTrace;
std::vector<T> m_values;
public:
void push(T value)
{
if(m_minTrace.empty())
m_minTrace.push_back(value);
else if(m_minTrace.back() > value)
m_minTrace.push_back(value);
else
m_minTrace.push_back(m_minTrace.back());
m_values.push_back(value);
}
T& top()
{
if(empty())
throw std::out_of_range("Attempted to call top() inside an empty stack");
return m_values.back();
}
T& min()
{
if(empty())
throw std::out_of_range("Attempted to call min() inside an empty stack");
return m_minTrace.back();
}
void pop()
{
if(empty())
throw std::out_of_range("Attempted to call pop() inside an empty stack");
m_minTrace.pop_back();
m_values.pop_back();
}
std::size_t size()
{
return m_values.size();
}
bool empty() { return size() == 0; }
};
template<typename T>
static std::ostream& operator<<(std::ostream& stream, std::initializer_list<T> values)
{
stream << "{ ";
for(decltype(values.size()) i = 0; const auto& value : values)
{
stream << value;
if(++i != values.size())
stream << ", ";
}
stream << " }";
return stream;
}
template<typename T>
static void run(std::initializer_list<T> values)
{
std::cout << "Input: " << values << "\n";
StackMin<T> stack;
for(const auto& value : values)
stack.push(value);
std::cout << "Output: { ";
while(!stack.empty())
{
std::cout << "[ " << stack.min() << ", " << stack.top() << " ]";
stack.pop();
if(stack.size())
std::cout << ", ";
}
std::cout << std::endl;
}
int main()
{
run<int>({ 4, 5, 1, 7, 8, 4, 2, 4, 5, 6, 7 });
return 0;
}