Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct CircularQueue
- {
- int *arr;
- int f;
- int r;
- int maxSize;
- int q_size;
- CircularQueue(int n)
- {
- arr = (int *)malloc(n * sizeof(arr));
- maxSize = n;
- f = -1;
- r = -1;
- q_size = 0;
- }
- bool isEmpty()
- {
- if(q_size == 0)
- return true;
- return false;
- }
- bool isFull()
- {
- if(q_size == maxSize) return true;
- return false;
- }
- void enqueue(int val)
- {
- if(isFull())
- {
- cout<<"Queue is full"<<endl;
- }
- else
- {
- r = (r + 1) % maxSize;
- arr[r] = val;
- q_size++;
- if(f == -1)
- f = 0;
- }
- }
- int dequeue()
- {
- if(isEmpty())
- {
- cout<<"Queue is Empty"<<endl;
- return -1;
- }
- else
- {
- int val = arr[f];
- if(f == r)
- {
- f = -1;
- r = -1;
- }
- else
- f = (f + 1) % maxSize;
- q_size--;
- return val;
- }
- }
- void q_clear()
- {
- f = -1;
- r = -1;
- q_size = 0;
- }
- void display()
- {
- if(isEmpty())
- {
- cout << "Queue is Empty" << endl;
- return;
- }
- for (int i = f; i != r; i = (i+1) % maxSize)
- cout << arr[i] << ' ';
- cout << arr[r] << endl;
- }
- void leftRotate(int x)
- {
- x = x % q_size;
- for (int i = 0; i < x; i++)
- {
- int temp = dequeue();
- enqueue(temp);
- }
- }
- void rightRotate(int x)
- {
- x = x % q_size;
- x = q_size - x;
- leftRotate(x);
- }
- };
- int main()
- {
- int n;
- printf("Enter the number of elements in the Circular Queue: ");
- scanf("%d",&n);
- CircularQueue q(n);
- printf("Enter %d elements: ", n);
- for (int i = 0; i < n; i++)
- {
- int x;
- cin >> x;
- q.enqueue(x);
- }
- q.display();
- int x = 3;
- q.rightRotate(3);
- q.display();
- }
Add Comment
Please, Sign In to add comment