#include <stdio.h>
#include <stdexcept>
using namespace std;
struct IStack {
virtual void push(int value) = 0; //добавить элемент value в стек, O(1)
virtual void pop() = 0; //удалить из стека последний добавленный элемент, O(1)
virtual int &top() = 0; //получить значение последнего добавленного элемента, O(1)
virtual int size() const = 0; //получить количество элементов в стеке, O(1)
virtual bool empty() const = 0; //проверить, пуст ли стек, O(1)
};
class ArrayStack : public IStack {
static const int MAX_SIZE = 100;
int a[MAX_SIZE], topIndex, elementsCount;
public:
ArrayStack() {
topIndex = elementsCount = 0;
}
void push(int value) {
if (elementsCount == MAX_SIZE)
throw runtime_error("Stack is full!");
a[topIndex++] = value;
elementsCount++;
}
void pop() {
if (empty())
throw runtime_error("Stack is empty!");
topIndex--;
elementsCount--;
}
int &top() {
if (empty())
throw runtime_error("Stack is empty!");
return a[topIndex - 1];
}
int size() const {
return elementsCount;
}
bool empty() const {
return elementsCount == 0;
}
};
int main() {
ArrayStack stack;
for (int i = 1; i <= 5; i++) {
stack.push(i);
}
for (int i = 1; i <= 5; i++) {
printf("%d ", stack.top());
stack.pop();
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRleGNlcHQ+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgSVN0YWNrIHsKICAgIHZpcnR1YWwgdm9pZCBwdXNoKGludCB2YWx1ZSkgPSAwOyAvL9C00L7QsdCw0LLQuNGC0Ywg0Y3Qu9C10LzQtdC90YIgdmFsdWUg0LIg0YHRgtC10LosICAgICAgICAgICAgICAgICAgICAgIE8oMSkKICAgIHZpcnR1YWwgdm9pZCBwb3AoKSA9IDA7ICAgICAgICAgICAvL9GD0LTQsNC70LjRgtGMINC40Lcg0YHRgtC10LrQsCDQv9C+0YHQu9C10LTQvdC40Lkg0LTQvtCx0LDQstC70LXQvdC90YvQuSDRjdC70LXQvNC10L3RgiwgICAgIE8oMSkKICAgIHZpcnR1YWwgaW50ICZ0b3AoKSA9IDA7ICAgICAgICAgICAv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