-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackWithExtremal.cpp
More file actions
36 lines (30 loc) · 1022 Bytes
/
Copy pathStackWithExtremal.cpp
File metadata and controls
36 lines (30 loc) · 1022 Bytes
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
// created by Maxim Busel @sajeruk
#include "StackWithExtremal.h"
template <typename T, typename Comparator>
void StackWithExtremal<T, Comparator>::push(const T& element) {
const T& extremalElement = (stackMin_.empty()) ?
element : std::min(element, stackMin_.top(), Comparator());
stackMin_.push(extremalElement);
stack_.push(element);
}
template <typename T, typename Comparator>
const T& StackWithExtremal<T, Comparator>::extremum() const {
return stackMin_.top();
}
template <typename T, typename Comparator>
bool StackWithExtremal<T, Comparator>::empty() const {
return stack_.empty();
}
template <typename T, typename Comparator>
const T& StackWithExtremal<T, Comparator>::top() const {
if (stack_.empty())
throw std::logic_error("Cannot top empty stack");
return stack_.top();
}
template <typename T, typename Comparator>
void StackWithExtremal<T, Comparator>::pop() {
if (stack_.empty())
throw std::logic_error("Cannot pop from empty stack");
stack_.pop();
stackMin_.pop();
}