#include <iostream>
#ifndef HELLO_WORLD_STACK_H
#define HELLO_WORLD_STACK_H
class stack {
private:
class elem {
public:
const int value;
elem* next;
elem(const int, elem*);
};
elem* topElem = nullptr;
public:
~stack();
void push(const int);
void pop();
const int top();
};
#endif //HELLO_WORLD_STACK_H+
stack::elem::elem(const int v, elem* n): value(v), next(n) {}
stack::~stack() {
elem* currentElem = topElem;
while (currentElem->next != nullptr) {
elem *toDelete = currentElem;
currentElem = currentElem->next;
delete toDelete;
}
delete currentElem;
}
void stack::push(const int i) {
topElem = new elem(i, topElem);
}
void stack::pop() {
if (topElem != nullptr) {
elem* toDelete = topElem;
topElem = topElem->next;
delete toDelete;
} else {throw "empty stack";}
}
const int stack::top() {
if (topElem != nullptr) {
return topElem->value;
}
throw "empty stack";
}
int main() {
auto s = new stack();
s->push(1);
s->push(2);
s->push(3);
s->push(4);
s->pop();
s->push(666);
std::cout << s->top() << std::endl;
delete s;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKI2lmbmRlZiBIRUxMT19XT1JMRF9TVEFDS19ICiNkZWZpbmUgSEVMTE9fV09STERfU1RBQ0tfSAoKY2xhc3Mgc3RhY2sgewoKcHJpdmF0ZToKCiAgICBjbGFzcyBlbGVtIHsKICAgIHB1YmxpYzoKICAgICAgICBjb25zdCBpbnQgdmFsdWU7CiAgICAgICAgZWxlbSogbmV4dDsKCiAgICAgICAgZWxlbShjb25zdCBpbnQsIGVsZW0qKTsKICAgIH07CgogICAgZWxlbSogdG9wRWxlbSA9IG51bGxwdHI7CgpwdWJsaWM6CiAgICB+c3RhY2soKTsKICAgIHZvaWQgcHVzaChjb25zdCBpbnQpOwogICAgdm9pZCBwb3AoKTsKICAgIGNvbnN0IGludCB0b3AoKTsKCn07CgoKI2VuZGlmIC8vSEVMTE9fV09STERfU1RBQ0tfSCsKCgpzdGFjazo6ZWxlbTo6ZWxlbShjb25zdCBpbnQgdiwgZWxlbSogbik6IHZhbHVlKHYpLCBuZXh0KG4pIHt9C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