tampurus

3.3 Circular Queue

Nov 25th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include<stdio.h>
  2. #define q_s 3
  3. #define r rear
  4. #define f front
  5. int r,f;
  6. int queue[q_s];
  7. void Insertion(int ele){
  8.     if( (r+1) % q_s == f ) {
  9.         printf("\nQueue is full");
  10.         return;
  11.     }
  12.     else if (r == -1 && f == -1){
  13.         r = f = 0;
  14.     }
  15.     else r =(r+1) % q_s;
  16.     queue[r] = ele;
  17. }
  18. void Deletion(){
  19.     if(r==-1) printf("\nQueue is already empty");
  20.     else if (r == f){
  21.         queue[f]=0;
  22.         r = f = -1;
  23.     }
  24.     else queue[f++] = 0;
  25. }
  26. void Print(){
  27.     if(r == -1) {
  28.         printf("\nQueue is empty");
  29.         return;
  30.     }
  31.     else{
  32.         int i;
  33.         for( i=f ; i != r ; i = (i+1)%q_s)
  34.             printf("%d ",queue[i]);
  35.         printf("%d ",queue[i]);
  36.     }
  37. }
  38. int main()
  39. {
  40.     r = f = -1;
  41.     int key,choice,element;
  42.     while(1){
  43.         printf("\n1 for insertion \n2 for deletion\nAny key to just print the queue\n");
  44.         scanf("%d",&choice);
  45.         if(choice==1){
  46.             printf("\nEnter the element ");
  47.             scanf("%d",&element);
  48.             Insertion(element);
  49.         }
  50.         else if(choice==2){
  51.             Deletion();
  52.         }
  53.         else Print();
  54.        
  55.         printf("\n1 - for repeat and any key to exit ");
  56.         scanf("%d",&key);
  57.         if(key!=1)break;
  58.     }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment