Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int *a, *b;
- int s, t;
- void push(int x)
- {
- s++;
- if (s > t)
- {
- t *= 2;
- b = new int[t];
- for (int i = 0; i < s; i++)
- {
- b[i] = a[i];
- }
- int *c = a;
- a = b;
- delete c;
- }
- a[s] = x;
- }
- int pop() {
- int res = a[s];
- if ((t > 4*s) && (t > 23))
- {
- t /= 2;
- b = new int[t];
- for (int i = 0; i < s; i++)
- {
- b[i] = a[i];
- }
- int *c = a;
- a = b;
- delete c;
- }
- s--;
- return res;
- }
- int main()
- {
- int n, x;
- char c;
- freopen("stack1.in", "r", stdin);
- freopen("stack1.out", "w", stdout);
- scanf("%d\n", &n);
- s = -1;
- t = 23;
- a = new int[t];
- for (int i = 0; i < n; i++)
- {
- scanf("%c", &c);
- if (c == '+')
- {
- scanf("%d\n", &x);
- push(x);
- } else
- {
- printf("%d\n", pop());
- scanf("\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement