Advertisement
Guest User

Untitled

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