#include <iostream>
#include <cstring>
#define SIZE 100000
class Stiva {
public:
//constructor of the class
Stiva() {
summit = 0;
container = new struct Node[SIZE];
}
//destructor of the class
~Stiva() {
delete [] container;
}
void Push(int value) {
if(summit == SIZE ) {
std::cout<<"The stack is full.";
return;
} else {
std::cout<<"Added!\n";
if(summit == 0) {
container[summit] = {value, value};
summit++;
} else {
container[summit] = {value, max(container[summit-1].max_value, value)};
summit++;
}
}
}
int max(int a, int b) {if(a>b) return a; else return b;}
int Top() {
if(!this->isEmpty()) return container[summit-1].value;
return -1;
}
int Max() {
if(!this->isEmpty()) return container[summit-1].max_value;
return -1;
}
int isEmpty() {
return summit == 0;
}
void Pop() {
if(!this->isEmpty()) summit--;
}
void display() {
for(int i = 0; i < summit; ++i) std::cout<<container[i].value<<"-"<<container[i].max_value<<"\n";
}
private:
struct Node {
int value,
max_value;
};
struct Node *container;
int summit;
};
int main(int argc, char const *argv[]) {
Stiva w;
w.Push(4);
w.Push(2);
w.Push(5);
w.Push(1);
std::cout << w.Top() << " " << w.Max() << "\n"; // afiseaza 1 5
w.Pop();
w.Pop();
std::cout << w.Top() << " " << w.Max() << "\n"; // afiseaza 2 4
return 0;
}
ICAjaW5jbHVkZSA8aW9zdHJlYW0+CiAgI2luY2x1ZGUgPGNzdHJpbmc+CiAgI2RlZmluZSBTSVpFIDEwMDAwMAoKICBjbGFzcyBTdGl2YSB7CgogICAgICAgIHB1YmxpYzoKICAgICAgICAvL2NvbnN0cnVjdG9yIG9mIHRoZSBjbGFzcwogICAgICAgIFN0aXZhKCkgewogICAgICAgICAgc3VtbWl0ID0gMDsKICAgICAgICAgIGNvbnRhaW5lciA9IG5ldyBzdHJ1Y3QgTm9kZVtTSVpFXTsKICAgICAgICB9CgogICAgICAgIC8vZGVzdHJ1Y3RvciBvZiB0aGUgY2xhc3MKICAgICAgICB+U3RpdmEoKSB7CiAgICAgICAgICBkZWxldGUgW10gY29udGFpbmVyOwogICAgICAgIH0KCiAgICAgICAgdm9pZCBQdXNoKGludCB2YWx1ZSkgewoKICAgICAgICAgICAgIGlmKHN1bW1pdCA9PSBTSVpFICkgewogICAgICAgICAgICAgICAgc3RkOjpjb3V0PDwiVGhlIHN0YWNrIGlzIGZ1bGwuIjsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBzdGQ6OmNvdXQ8PCJBZGRlZCFcbiI7CiAgICAgICAgICAgICAgICBpZihzdW1taXQgPT0gMCkgewogICAgICAgICAgICAgICAgICBjb250YWluZXJbc3VtbWl0XSA9IHt2YWx1ZSwgdmFsdWV9OwogICAgICAgICAgICAgICAgICBzdW1taXQrKzsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIGNvbnRhaW5lcltzdW1taXRdID0ge3ZhbHVlLCBtYXgoY29udGFpbmVyW3N1bW1pdC0xXS5tYXhfdmFsdWUsIHZhbHVlKX07CiAgICAgICAgICAgICAgICAgIHN1bW1pdCsrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpbnQgbWF4KGludCBhLCBpbnQgYikge2lmKGE+YikgcmV0dXJuIGE7IGVsc2UgcmV0dXJuIGI7fQogICAgICAgIGludCBUb3AoKSB7CiAgICAgICAgICAgIGlmKCF0aGlzLT5pc0VtcHR5KCkpIHJldHVybiBjb250YWluZXJbc3VtbWl0LTFdLnZhbHVlOwogICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgfQoKICAgICAgICBpbnQgTWF4KCkgewogICAgICAgICAgICBpZighdGhpcy0+aXNFbXB0eSgpKSByZXR1cm4gY29udGFpbmVyW3N1bW1pdC0xXS5tYXhfdmFsdWU7CiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CgogICAgICAgIGludCBpc0VtcHR5KCkgewogICAgICAgICAgcmV0dXJuIHN1bW1pdCA9PSAwOwogICAgICAgIH0KCiAgICAgICAgdm9pZCBQb3AoK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