irapilguy

Untitled

Nov 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4. struct El {
  5.     int data;
  6.     El * next;
  7. };
  8. int countt = 0;
  9. El * top = NULL;
  10. El * createEl(int x) {
  11.     El * t = new El();
  12.     t->data = x;
  13.     t->next = NULL;
  14.     return t;
  15. }
  16. void push(int x) {
  17.     El * t = createEl(x);
  18.     if (top == NULL) {
  19.         top = t;
  20.         return;
  21.     }
  22.     t->next = top;
  23.     top = t;
  24. }
  25. void pop() {
  26.     if (top == NULL) return;
  27.     top = top->next;
  28.     if (countt > 0) countt = countt - top->data;
  29. }
  30. int main() {
  31.     int n, Q, x;
  32.     cin >> n;
  33.     for (int i = 0; i < n; i++) {
  34.         cin >> Q;
  35.         if (Q == 0) {
  36.             pop();
  37.         }
  38.         if (Q == 1) {
  39.             cin >> x;
  40.             push(x);
  41.             countt += x;
  42.         }
  43.         if (Q == 2) {
  44.             cout << countt << "\n";
  45.         }
  46.     }
  47.     return 0;
  48. }
Add Comment
Please, Sign In to add comment