Facebook
From Gamboge Leech, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 68
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.         int data;
  6.         Node* next = nullptr;
  7.  
  8.         Node(int data, Node* next = nullptr) {
  9.                 this->data = data;
  10.                 this->next = next;
  11.         }
  12. };
  13.  
  14. class Stack {
  15.  
  16. public:
  17.         Node* top = nullptr;
  18.  
  19.         void push(int number) {
  20.                 if (isEmpty()) {
  21.                         top = new Node(number);
  22.                 }
  23.                 else {
  24.                         Node* newTop = new Node(number, top);
  25.                         top = newTop;
  26.                 }
  27.         }
  28.  
  29.         void pop() {
  30.                 if (!isEmpty()) {
  31.                         Node* oldTop = top;
  32.                         top = top->next;
  33.                         delete oldTop;
  34.                 }
  35.                 else {
  36.                         throw std::underflow_error("Stack is empty");
  37.                 }
  38.         }
  39.  
  40.         int peek() const {
  41.                 if (!isEmpty()) {
  42.                         return top->data;
  43.                 }
  44.                 else {
  45.                         throw std::underflow_error("Stack is empty");
  46.                 }
  47.         }
  48.  
  49.         bool isEmpty() const {
  50.                 return top == nullptr;
  51.         }
  52. };
  53.  
  54. void PrintStack(Stack s)
  55. {
  56.         if (s.isEmpty()) return;
  57.  
  58.         int x = s.peek();
  59.         s.pop();
  60.         PrintStack(s);
  61.         cout << x << " ";
  62.         s.push(x);
  63. }
  64.  
  65. Stack s;
  66. int main() {
  67.         int n;
  68.         cin >> n;
  69.         int num;
  70.         if (n > 0) {
  71.                 cin >> num;
  72.                 s.push(num);
  73.         }
  74.         bool  flag = 0;
  75.  
  76.         if (n > 1) {
  77.                 cin >> num;
  78.         }
  79.         for (int i = 2; i <= n; )
  80.         {
  81.  
  82.                 while (s.peek() > 0) {
  83.                         if (s.peek() + num == 0)
  84.                         {
  85.                                 if (!s.isEmpty()) {
  86.                                         s.pop();
  87.                                         i++;
  88.                                         if (i > n) { flag = 1; break; }
  89.                                         cin >> num;
  90.                                         continue;
  91.                                 }
  92.                         }
  93.                         while (!s.isEmpty() && s.peek() + num < 0 && s.peek()>0) {
  94.                                 if (!s.isEmpty())    s.pop();
  95.  
  96.                         }
  97.                         if (num > 0) {
  98.                                 s.push(num);
  99.                         }
  100.                         if ((s.isEmpty() && num < 0) || (num < 0 && s.peek() < 0)) {
  101.                                 s.push(num);
  102.                         }
  103.                         i++;
  104.                         if (i > n) { flag = 1; break; }
  105.                         cin >> num;
  106.                 }
  107.                 if (flag) break;
  108.                 while (s.peek() < 0)
  109.                 {
  110.                         s.push(num);
  111.                         i++;
  112.                         if (i > n) break;
  113.                         cin >> num;
  114.                 }
  115.         }
  116.  
  117.         if (!s.isEmpty()) PrintStack(s);
  118.         else cout << '\n';
  119. }