Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #define size 5
  3.  
  4. int queue[5][2] = {0};
  5.  
  6. int top = -1;
  7.  
  8. int bottom;
  9.  
  10. void push(int value, int pr)
  11. {
  12.  
  13.     int i, j, k;
  14.     if (top < size - 1)
  15.     {
  16.         if (queue[top][1] > pr)
  17.         {
  18.             for (i = 0; i < top; i++)
  19.             {
  20.                 if (queue[i][1] > pr)
  21.                 {
  22.                     break;
  23.                 }
  24.             }
  25.             for (j = top; j >= i; j--)
  26.             {
  27.                 queue[j + 1][0] = queue[j][0];
  28.                 queue[j + 1][1] = queue[j][1];
  29.             }
  30.  
  31.             top++;
  32.             queue[i][0] = value;
  33.             queue[i][1] = pr;
  34.  
  35.         } else
  36.         {
  37.             top++;
  38.             queue[top][0] = value;
  39.             queue[top][1] = pr;
  40.         }
  41.  
  42.     } else
  43.     {
  44.         printf("queue overflow \n");
  45.     }
  46. }
  47.  
  48. void pop()
  49. {
  50.     int i;
  51.     if (queue[0][0] == 0)
  52.     {
  53.         printf("\n The queue is empty  \n");
  54.     } else
  55.     {
  56.         printf("After , dequeue the following value is erased \n  %d \n", queue[0][0]);
  57.         for (i = 0; i < top; i++)
  58.         {
  59.             queue[i][0] = queue[i + 1][0];
  60.             queue[i][1] = queue[i + 1][1];
  61.         }
  62.         queue[top][0] = 0;
  63.         queue[top][1] = 0;
  64.         top--;
  65.     }
  66. }
  67.  
  68. void display()
  69. {
  70.     int i, j;
  71.     printf("Element\tPriority \n");
  72.     for (i = size - 1; i >= 0; i--)
  73.     {
  74.         for (j = 0; j < 2; j++)
  75.         {
  76.             printf(" %d\t", queue[i][j]);
  77.         }
  78.         printf("\n");
  79.     }
  80. }
  81.  
  82. int main()
  83. {
  84.     int i, j, ch = 0, value = 0, pr = 0;
  85.     while (1)
  86.     {
  87.         printf("\n Please Enter the choice. \n");
  88.         printf("1 for Enqueue \n 2 for Dequeue \n 3 for display\n  5 for exit: \t \n");
  89.         scanf("%d", & ch);
  90.         switch (ch)
  91.         {
  92.         case 1:
  93.             printf("\n Please Enter the number to be inserted: \t ");
  94.             scanf("%d", & value);
  95.             printf("\n Please Enter the priority: \t ");
  96.             scanf("%d", & pr);
  97.             push(value, pr);
  98.             break;
  99.  
  100.         case 2:
  101.             pop();
  102.             break;
  103.  
  104.         case 3:
  105.             display();
  106.             break;
  107.  
  108.         case 5:
  109.             return 0;
  110.  
  111.         default:
  112.             printf("You entered wrong choice\n");
  113.  
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement