JayBawankar

dspd circular queue

Aug 1st, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. //Program for circular queue
  2.  #include<stdio.h>
  3.  #include<conio.h>
  4.  int front,rear;
  5.  void insertion(int a[],int size)
  6.  {
  7.     int elem;
  8.     if((rear+1)%size==front)
  9.     {
  10.         printf("\n Overflow.\n");
  11.         getch();
  12.         return;
  13.     }
  14.     printf("\n Enter the element for queue : ");
  15.     scanf("%d",&elem);
  16.     rear=(rear+1)%size;
  17.     a[rear]=elem;
  18.     if(front==-1)
  19.         front=0;
  20.     return;
  21.  }
  22.  void deletion(int a[],int size)
  23.  {
  24.     if(front==-1)
  25.     {
  26.         printf("\n Underflow.\n");
  27.         getch();
  28.         return;
  29.     }
  30.     if(front==rear)
  31.     {
  32.         printf("\n The element to be deleted is %d",a[front]);
  33.         getch();
  34.         front=rear=-1;
  35.         return;
  36.     }
  37.     printf("\n The element to be deleted is %d",a[front]);
  38.     front=(front+1)%size;
  39.     getch();
  40.     return;
  41.  }
  42.  void print(int a[],int size)
  43.  {
  44.     int i;
  45.     if(rear==-1)
  46.     {
  47.         printf("\n No elements in queue.\n");
  48.         getch();
  49.         return;
  50.     }
  51.     i=front;
  52.     while(i!=rear)
  53.     {
  54.         printf(" %d",a[i]);
  55.         i=(i+1)%size;
  56.     }
  57.     printf(" %d",a[i]);
  58.     getch();
  59.     return;
  60.  }
  61.  void main()
  62.  {
  63.     int a[15],size,ch;
  64.     clrscr();
  65.     printf("\n Enter the size of the queue : ");
  66.     scanf("%d",&size);
  67.     front=rear=-1;
  68.     while(ch!=4)
  69.     {
  70.         printf("\n\n\t MENU");
  71.         printf("\n\t1.INSERTION");
  72.         printf("\n\t2.DELETION");
  73.         printf("\n\t3.DISPLAY");
  74.         printf("\n\t4.EXIT");
  75.         printf("\n Enter your choice : ");
  76.         scanf("%d",&ch);
  77.         switch(ch)
  78.         {
  79.             case 1:insertion(a,size); break;
  80.             case 2:deletion(a,size);  break;
  81.             case 3:print(a,size);  break;
  82.             case 4:exit(0);
  83.             default:printf("\n Sorry wrong input.");
  84.         }
  85.         clrscr();
  86.     }
  87.     getch();
  88.  }
Add Comment
Please, Sign In to add comment