Advertisement
den4ik2003

Untitled

Dec 23rd, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const int cst = -100;
  4.  
  5. struct Element {
  6. int value, size = 0;
  7. Element *prev = nullptr; // prev указывает на element
  8. };
  9.  
  10. struct Stack {
  11. Element *p = new Element();
  12.  
  13. void push(int n) {
  14. Element *s = new Element();
  15. s->value = n;
  16. s->prev = p;
  17. s->size = p->size + 1;
  18. p = s;
  19. std::cout << "ok\n";
  20. }
  21.  
  22. int back() {
  23. if (p->size == 0) {
  24. return cst;
  25. }
  26. return p->value;
  27. }
  28.  
  29. int pop() {
  30. if (p->size == 0) {
  31. return cst;
  32. }
  33. Element *s = p;
  34. int x = p->value;
  35. p = p->prev;
  36. delete s;
  37. return x;
  38. }
  39.  
  40. void size() {
  41. std:: cout << p->size <<"\n";
  42. }
  43.  
  44. void clear() {
  45. delete p;
  46. p = new Element();
  47. std::cout << "ok\n";
  48. }
  49.  
  50. };
  51.  
  52. int main() {
  53. std::ios_base::sync_with_stdio(false);
  54. std::cin.tie(NULL);
  55.  
  56.  
  57. Stack *n1 = new Stack();
  58. std::string l;
  59. std::cin >> l;
  60. while(l != "exit") {
  61.  
  62. if(l == "back") {
  63. int x = n1->back();
  64. if(x != cst) {
  65. std::cout << x << "\n";
  66. }
  67. else {
  68. std::cout << "error\n";
  69. }
  70. }
  71.  
  72. if(l == "push") {
  73. int x;
  74. std::cin >> x;
  75. n1->push(x);
  76. }
  77. if(l == "pop") {
  78. int x = n1->pop();
  79. if(x != cst) {
  80. std::cout << x << "\n";
  81. }
  82. else {
  83. std::cout << "error\n";
  84. }
  85. }
  86. if(l == "size") {
  87. n1->size();
  88. }
  89. if(l == "clear") {
  90. n1->clear();
  91. }
  92. std::cin >> l;
  93. }
  94. std::cout << "bye\n";
  95. delete n1;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement