Advertisement
Void-voiD

Untitled

Dec 15th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Stack {
  6. int *data, cap, top1, top2, max;
  7. };
  8.  
  9. struct Stack InitStack(int n)
  10. {
  11. struct Stack s;
  12. int *d = (int*)malloc(n * sizeof(int));
  13. s.data = d;
  14. s.cap = n;
  15. s.top1 = 0;
  16. s.top2 = n - 1;
  17. s.max = -2000000000;
  18. return s;
  19. }
  20.  
  21. int StackEmpty1(struct Stack s)
  22. {
  23. return s.top1 == 0;
  24. }
  25.  
  26. int StackEmpty2(struct Stack s)
  27. {
  28. return s.top2 == s.cap - 1;
  29. }
  30.  
  31. void Push1(struct Stack s, int x)
  32. {
  33. if (s.top2 < s.top1) printf("Переполнение\n");
  34. s.data[s.top1] = x;
  35. s.top1++;
  36. }
  37.  
  38. void Push2(struct Stack s, int x)
  39. {
  40. if (s.top2 < s.top1) printf("Переполнение\n");
  41. s.data[s.top2] = x;
  42. s.top2--;
  43. }
  44.  
  45. int Pop1(struct Stack s)
  46. {
  47. if (StackEmpty1(s)) printf("NULL\n");
  48. s.top1--;
  49. return s.data[s.top1];
  50. }
  51.  
  52. int Pop2(struct Stack s)
  53. {
  54. if (StackEmpty2(s)) printf("NULL\n");
  55. s.top2++;
  56. return s.data[s.top2];
  57. }
  58.  
  59. struct Stack InitQueue(int n)
  60. {
  61. return InitStack(n);
  62. }
  63.  
  64. int QueueEmpty(struct Stack s)
  65. {
  66. return (StackEmpty1(s) && StackEmpty2(s));
  67. }
  68.  
  69. struct Stack Enqueue(struct Stack s, int x)
  70. {
  71. Push1(s, x);
  72. s.top1++;
  73. if (x > s.max) s.max = x;
  74. return s;
  75. }
  76.  
  77. int *Dequeue(struct Stack s)
  78. {
  79. int cur = 0;
  80. int *res = (int*)malloc(2 * sizeof(int));
  81. if (StackEmpty2(s)) {
  82. while (!(StackEmpty1(s))) {
  83. Push2(s, Pop1(s));
  84. s.top1--;
  85. s.top2--;
  86. cur++;
  87. }
  88. }
  89. res[0] = cur;
  90. res[1] = Pop2(s);
  91. return res;
  92. }
  93.  
  94. int main()
  95. {
  96. int j, i, n, x, *cur;
  97. char ss[8];
  98. scanf("%d\n", &n);
  99. struct Stack s = InitQueue(2 * n);
  100. for (i = 0; i < n; i++) {
  101. scanf("%s", ss);
  102. if (0 == strcmp(ss, "ENQ")) {
  103. scanf(" %d", &x);
  104. s = Enqueue(s, x);
  105. }
  106. if (0 == strcmp(ss, "EMPTY")) {
  107. if (QueueEmpty(s)) printf("true\n");
  108. else printf("false\n");
  109. }
  110. if (0 == strcmp(ss, "DEQ")) {
  111. cur = Dequeue(s);
  112. s.top1 -= cur[0];
  113. s.top2 -= cur[0];
  114. s.top2++;
  115. if (cur[1] == s.max) {
  116. s.max = -2000000000;
  117. for (j = s.top2 + 1; j < s.cap; j++) {
  118. if (s.data[j] > s.max) s.max = s.data[j];
  119. }
  120. }
  121. printf("%d\n", cur[1]);
  122. free(cur);
  123. }
  124. if (0 == strcmp(ss, "MAX")) {
  125. printf("%d\n", s.max);
  126. }
  127. if (i != n - 1) scanf("\n");
  128. }
  129. free(s.data);
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement