Advertisement
ppathak35

circular queue

Jun 28th, 2022 (edited)
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. // Circular Queue
  2.  
  3. #include <stdio.h>
  4.  
  5. // macros
  6. #define maxsize 5
  7.  
  8. // function declarations
  9. void insert(int);
  10. void display();
  11. int isEmpty();
  12.  
  13.  
  14. // Global Variables
  15. int front=-1, rear=-1;
  16. int arr[maxsize];
  17.  
  18. // Main Function
  19. int main(){
  20.     int choice=0, element=0;
  21.     while (1) {
  22.         printf("1. Insert Element\n");
  23.         printf("2. Display Queue\n");
  24.         printf("3. Delete Element\n");
  25.         printf("4. Queue Size\n");
  26.         printf("5. Exit\n");
  27.        
  28.         printf("\nEnter choice : ");
  29.         scanf("%d", &choice);
  30.        
  31.         switch(choice) {
  32.             case 1 : printf("Insert Data : ");
  33.                      scanf("%d", &element);
  34.                      insert(element);
  35.             case 2 : display();
  36.                      break;
  37.             case 3 : break;
  38.             case 4 : break;
  39.             case 5 : exit(0);
  40.                      break;
  41.             default: break;
  42.         }
  43.     }
  44. }
  45.  
  46. int isEmpty() {
  47.     return rear == -1;
  48. }
  49.  
  50. // Function Definitions
  51. void insert(int ele) {
  52.     if (front == -1) {
  53.         front = rear = 0;
  54.     }
  55.    
  56.     if (rear == maxsize - 1){
  57.         rear = 0;
  58.     }
  59.     else {
  60.         rear += 1;
  61.     }
  62.    
  63.     arr[rear] = ele;
  64. }
  65.  
  66. void display() {
  67.     for (int i=0; i<=maxsize-1; i++) {
  68.         printf("%d", arr[i]);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement