Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. #define MAX 50
  5.  
  6. void qinsert();
  7. void qremove();
  8. void display();
  9. int queue[MAX];
  10. int rear = - 1;
  11. int front = - 1;
  12. main()
  13. {
  14.     char choice;
  15.     while (1)
  16.     {
  17.         printf("U.Insert element to queue \n");
  18.         printf("O.Eemove element from queue \n");
  19.         printf("D.Display all elements of queue \n");
  20.         printf("E.Quit \n");
  21.         printf("Enter your choice : ");
  22.         scanf(" %c", &choice);
  23.         switch (choice)
  24.         {
  25.             case 'U':
  26.             qinsert();
  27.             break;
  28.             case 'O':
  29.             qremove();
  30.             break;
  31.             case 'D':
  32.             display();
  33.             break;
  34.             case 'E':
  35.             exit(1);
  36.             default:
  37.             printf("Wrong character! \n");
  38.         } /* End of switch */
  39.     } /* End of while */
  40. } /* End of main() */
  41.  
  42. void qinsert()
  43. {
  44.     int add;
  45.     if (rear == MAX - 1)
  46.     printf("Queue Overflow \n");
  47.     else
  48.     {
  49.         if (front == - 1)
  50.         /*If queue is initially empty */
  51.         front = 0;
  52.         printf("Inset the element in queue : ");
  53.         scanf("%d", &add);
  54.         rear = rear + 1;
  55.         queue[rear] = add;
  56.     }
  57. } /* End of qinsert() */
  58.  
  59. void qremove()
  60. {
  61.     if (front == - 1 || front > rear)
  62.     {
  63.         printf("Queue Underflow \n");
  64.         return ;
  65.     }
  66.     else
  67.     {
  68.         printf("Element qremoved from queue is : %d\n", queue[front]);
  69.         front = front + 1;
  70.     }
  71. } /* End of qremove() */
  72.  
  73. void display()
  74. {
  75.     int i;
  76.     if (front == - 1)
  77.         printf("Queue is empty \n");
  78.     else
  79.     {
  80.         printf("Queue is : \n");
  81.         for (i = front; i <= rear; i++)
  82.             printf("%d ", queue[i]);
  83.         printf("\n");
  84.     }
  85. } /* End of display() */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement