upsidedown

linear queue

Sep 20th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5. #define MAX 50
  6. struct QUEUE
  7. {
  8.     int data[MAX];
  9.     int front, rear;
  10. }q;
  11.  
  12. void print()
  13. {
  14.     int i;
  15.     printf("\nthe queue is \t front->");
  16.     for(i=q.front;i<=q.rear;i++)
  17.         printf("%d->",q.data[i]);
  18.     printf("Rear");
  19. }
  20.  
  21.  
  22. int main()
  23. {
  24.    
  25.    
  26.     int ch,x;
  27.     q.front=0;
  28.     q.rear=-1;
  29.  
  30.     do
  31.     {
  32.         printf("\n\n1.insert\n2.delete\n3.display\n4.exit\n enter ch: ");
  33.         scanf("%d",&ch);
  34.  
  35.         switch(ch)
  36.         {
  37.         case 1:
  38.             if(q.rear==MAX)
  39.             {
  40.                 printf("\nQ full");
  41.             }
  42.             else
  43.             {
  44.                 printf("Enter the element to add");
  45.                 scanf("%d",&x);
  46.    
  47.                 q.rear++;
  48.                 q.data[q.rear]=x;
  49.             }
  50.             print();
  51.             break;
  52.  
  53.         case 2:
  54.             if(q.front> q.rear)
  55.             {
  56.                 printf("q empty");
  57.             }
  58.             else
  59.             {
  60.                 x=q.front;
  61.                 q.front++;
  62.                 printf("\n%d deleted",x);
  63.             }
  64.             print();
  65.             break;
  66.  
  67.         case 3:
  68.             print();
  69.             break;
  70.         case 4:
  71.             break;
  72.         }
  73.     }while(ch!=4);
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment