splash365

circular queue (array)

Jan 11th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct CircularQueue
  5. {
  6.     int *arr;
  7.     int f;
  8.     int r;
  9.     int maxSize;
  10.     int q_size;
  11.     CircularQueue(int n)
  12.     {
  13.         arr = (int *)malloc(n * sizeof(arr));
  14.         maxSize = n;
  15.         f = -1;
  16.         r = -1;
  17.         q_size = 0;
  18.     }
  19.     bool isEmpty()
  20.     {
  21.         if(q_size == 0)
  22.             return true;
  23.         return false;
  24.     }
  25.     bool isFull()
  26.     {
  27.         if(q_size == maxSize) return true;
  28.         return false;
  29.     }
  30.     void enqueue(int val)
  31.     {
  32.         if(isFull())
  33.         {
  34.             cout<<"Queue is full"<<endl;
  35.         }
  36.         else
  37.         {
  38.             r = (r + 1) % maxSize;
  39.             arr[r] = val;
  40.             q_size++;
  41.             if(f == -1)
  42.                 f  = 0;
  43.         }
  44.     }
  45.     int dequeue()
  46.     {
  47.         if(isEmpty())
  48.         {
  49.             cout<<"Queue is Empty"<<endl;
  50.             return -1;
  51.         }
  52.         else
  53.         {
  54.             int val = arr[f];
  55.             if(f == r)
  56.             {
  57.                 f = -1;
  58.                 r = -1;
  59.             }
  60.             else
  61.                 f = (f + 1) % maxSize;
  62.             q_size--;
  63.             return val;
  64.         }
  65.     }
  66.     void q_clear()
  67.     {
  68.         f = -1;
  69.         r = -1;
  70.         q_size = 0;
  71.     }
  72.     void display()
  73.     {
  74.         if(isEmpty())
  75.         {
  76.             cout << "Queue is Empty" << endl;
  77.             return;
  78.         }
  79.         for (int i = f; i != r; i = (i+1) % maxSize)
  80.             cout << arr[i] << ' ';
  81.         cout << arr[r] << endl;
  82.     }
  83.     void leftRotate(int x)
  84.     {
  85.         x = x % q_size;
  86.         for (int i = 0; i < x; i++)
  87.         {
  88.             int temp = dequeue();
  89.             enqueue(temp);
  90.         }
  91.     }
  92.     void rightRotate(int x)
  93.     {
  94.         x = x % q_size;
  95.         x = q_size - x;
  96.         leftRotate(x);
  97.     }
  98. };
  99.  
  100. int main()
  101. {
  102.     int n;
  103.     printf("Enter the number of elements in the Circular Queue: ");
  104.     scanf("%d",&n);
  105.     CircularQueue q(n);
  106.     printf("Enter %d elements: ", n);
  107.     for (int i = 0; i < n; i++)
  108.     {
  109.         int x;
  110.         cin >> x;
  111.         q.enqueue(x);
  112.     }
  113.     q.display();
  114.     int x = 3;
  115.     q.rightRotate(3);
  116.     q.display();
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
Add Comment
Please, Sign In to add comment