Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int *a, *b;
  6. int s, t;
  7.  
  8. void push(int x)
  9. {
  10. s++;
  11.  
  12. if (s > t)
  13. {
  14. t *= 2;
  15.  
  16. b = new int[t];
  17.  
  18. for (int i = 0; i < s; i++)
  19. {
  20. b[i] = a[i];
  21. }
  22.  
  23. int *c = a;
  24. a = b;
  25. delete c;
  26. }
  27.  
  28. a[s] = x;
  29. }
  30.  
  31. int pop() {
  32. int res = a[s];
  33.  
  34. if ((t > 4*s) && (t > 23))
  35. {
  36. t /= 2;
  37.  
  38. b = new int[t];
  39.  
  40. for (int i = 0; i < s; i++)
  41. {
  42. b[i] = a[i];
  43. }
  44.  
  45. int *c = a;
  46. a = b;
  47. delete c;
  48. }
  49.  
  50. s--;
  51.  
  52. return res;
  53. }
  54.  
  55. int main()
  56. {
  57. int n, x;
  58. char c;
  59.  
  60. freopen("stack1.in", "r", stdin);
  61. freopen("stack1.out", "w", stdout);
  62.  
  63. scanf("%d\n", &n);
  64.  
  65. s = -1;
  66. t = 23;
  67.  
  68. a = new int[t];
  69.  
  70. for (int i = 0; i < n; i++)
  71. {
  72. scanf("%c", &c);
  73. if (c == '+')
  74. {
  75. scanf("%d\n", &x);
  76. push(x);
  77. } else
  78. {
  79. printf("%d\n", pop());
  80. scanf("\n");
  81. }
  82. }
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement