Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const int cst = -100;
- struct Element {
- int value, size = 0;
- Element *prev = nullptr; // prev указывает на element
- };
- struct Stack {
- Element *p = new Element();
- void push(int n) {
- Element *s = new Element();
- s->value = n;
- s->prev = p;
- s->size = p->size + 1;
- p = s;
- std::cout << "ok\n";
- }
- int back() {
- if (p->size == 0) {
- return cst;
- }
- return p->value;
- }
- int pop() {
- if (p->size == 0) {
- return cst;
- }
- Element *s = p;
- int x = p->value;
- p = p->prev;
- delete s;
- return x;
- }
- void size() {
- std:: cout << p->size <<"\n";
- }
- void clear() {
- delete p;
- p = new Element();
- std::cout << "ok\n";
- }
- };
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(NULL);
- Stack *n1 = new Stack();
- std::string l;
- std::cin >> l;
- while(l != "exit") {
- if(l == "back") {
- int x = n1->back();
- if(x != cst) {
- std::cout << x << "\n";
- }
- else {
- std::cout << "error\n";
- }
- }
- if(l == "push") {
- int x;
- std::cin >> x;
- n1->push(x);
- }
- if(l == "pop") {
- int x = n1->pop();
- if(x != cst) {
- std::cout << x << "\n";
- }
- else {
- std::cout << "error\n";
- }
- }
- if(l == "size") {
- n1->size();
- }
- if(l == "clear") {
- n1->clear();
- }
- std::cin >> l;
- }
- std::cout << "bye\n";
- delete n1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement