Advertisement
d1i2p3a4k5

5.Priority Queue using array (DS)

Oct 30th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #define max 100
  4. typedef struct
  5. {
  6.     int q[max];
  7.     int f,r;
  8. }queue;
  9. int isempty(queue *t)
  10. {
  11.     if(t->f==t->r+1)
  12.         return 1;
  13.     else return 0;
  14. }
  15. void insert(queue *t,int ele)
  16. {
  17.     if(t->r==max-1)
  18.     {
  19.         printf("\nQUEUE OVERFLOW\n");
  20.         return ;
  21.     }
  22.     t->r++;
  23.     t->q[t->r] = ele;
  24.     return;
  25. }
  26. void delete1(queue *t)
  27. {
  28.     int i,max1,pos;
  29.     if(isempty(t)==1)
  30.     {
  31.         printf("\nQUEUE UNDERFLOW\n");
  32.         return ;
  33.     }
  34.     max1 = t->q[t->f];
  35.     pos = t->f;
  36.     for(i=t->f+1;i<=t->r;i++)
  37.     {
  38.         if(max1<(t->q[i]))
  39.         {
  40.             max1=t->q[i];
  41.             pos=i;
  42.         }
  43.     }
  44.     for(i=pos;i<t->r;i++)
  45.     {
  46.         t->q[i]=t->q[i+1];
  47.     }
  48.     t->r--;
  49.     printf("Deleted element is %d",max1);
  50.     return;
  51. }
  52. void queuefront(queue *t)
  53. {
  54.     int z;
  55.     if(isempty(t)==1)
  56.     {
  57.         printf("\nQUEUE UNDERFLOW");
  58.         return;
  59.     }
  60.     z  = t->q[t->f];
  61.     printf("QUEUEFRONT is %d",z);
  62.     return;
  63. }
  64. void display(queue *t)
  65. {
  66.     int i;
  67.     if(isempty(t)==1)
  68.     {
  69.         printf("\nQUEUE UNDERFLOW");
  70.         return;
  71.     }
  72.     printf("\nELEMENT OF QUEUE ARE SHOWN BELOW\n");
  73.     for(i=t->f;i<=t->r;i++)
  74.     {
  75.         printf("%d\t",t->q[i]);
  76.     }
  77.     return ;
  78. }
  79. int  main()
  80. {
  81.     int ch,ele;
  82.     queue x;
  83.     x.f = 0;
  84.     x.r = -1;
  85.     printf("IMPLEMENTATION OF PRIORITY QUEUE ");
  86.     while(1)
  87.     {
  88.         printf("\nENTER CHOICE \n1.INSERT 2.DELETE 3.QUEUEFRONT 4.DISPLAY 5.EXIT\n");
  89.         scanf_s("%d",&ch);
  90.         if(ch==5)
  91.         {
  92.             break;
  93.         }
  94.         switch(ch)
  95.         {
  96.         case 1:printf("\nEnter element to insert\n");
  97.             scanf_s("%d",&ele);
  98.             insert(&x,ele);
  99.             break;
  100.         case 2:delete1(&x);
  101.             break;
  102.         case 3:queuefront(&x);
  103.             break;
  104.         case 4:display(&x);
  105.             break;
  106.         default:printf("\nINVALID INPUT");
  107.         }
  108.     }
  109.     _getch();
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement