Repository navigation
Expand file tree
/
Copy pathStackWithMaxVer2.cpp
More file actions
111 lines (92 loc) · 2.17 KB
/
Copy pathStackWithMaxVer2.cpp
File metadata and controls
111 lines (92 loc) · 2.17 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <stack>
template <class T>
class StackWithMax {
std::stack<T> steck, steck_max;
public:
StackWithMax() {}
StackWithMax(const std::stack <T>& other) {
steck = other;
steck_max = other.steck_max;
}
StackWithMax<T>& operator=(const std::stack<T>& other) {
steck = other.steck;
steck_max = other.max_steck;
return *this;
}
int size() {
return steck.size();
}
void pop() {
if (steck.size() == 0)
throw "ERROR: this steck is empty";
else {
if (steck.top() == steck_max.top()) {
steck_max.pop();
steck.pop();
}
else if (steck.top() != steck_max.top()) {
steck.pop();
}
}
}
void push(const T& value) {
if (steck.size() == 0 || steck_max.top() < value) {
steck_max.push(value);
steck.push(value);
}
else if (steck.size() > 0 || steck_max.top() >= value)
steck.push(value);
}
T top() {
if (size() == 0) {
throw "ERROR: this steck is empty";
return T();
}
return steck.pop();
}
T maxelem() {
if (size() == 0) {
throw "ERROR: this steck is empty";
return T();
}
return steck_max.top();
}
void print() { // how can i do this?
StackWithMax<int> temp = *this;
for (int count = 0; count < temp.size(); count++) {
std::cout << temp.steck.top() << "\t";
temp.pop();
}
}
friend std::ostream& operator<<(std::ostream& out, StackWithMax<int>& other) // how can i do this?
{
StackWithMax<int> temp = other;
for (int count = 0; count < temp.size(); count++) {
out << temp.steck.top() << "\t";
temp.pop();
}
return out;
}
};
int main() {
StackWithMax<int> steck1;
StackWithMax<int> steck2;
for (int count = 0; count < 100; count++) {
steck1.push(count);
}
steck1.push(123);
std::cout << "MAX elem of steck1 is " << steck1.maxelem() << std::endl;
steck1.push(123);
std::cout << steck1 << std::endl;
std::cout << "MAX elem of steck1 is " << steck1.maxelem() << std::endl;
//st.print();
steck1.push(123);
std::cout << "MAX elem of steck1 is " << steck1.maxelem() << std::endl;
std::cout << steck1 << std::endl;
steck2 = steck1;
steck1.pop();
std::cout << steck2;
std::cout << "MAX elem of steck2 is " << steck2.maxelem() << std::endl;
return 0;
}