Circular Queue
Last updated
Was this helpful?
Last updated
Was this helpful?
#include <bits/stdc++.h>
class CircularQueue{
int *arr;
int size;
int front, rear, elements;
public:
// Initialize your data structure.
CircularQueue(int n){
size = n;
arr = new int[size];
front = rear = elements = 0;
}
bool isEmpty() const {
return elements == 0;
}
bool isFull() const {
return elements == size;
}
// Enqueues 'X' into the queue. Returns true if it gets pushed into the stack, and false otherwise.
bool enqueue(int value){
if(isFull())
return false;
rear %= size;
arr[rear++] = value;
elements++;
return true;
}
// Dequeues top element from queue. Returns -1 if the stack is empty, otherwise returns the popped element.
int dequeue(){
if(isEmpty())
return -1;
front %= size;
int data = arr[front++];
elements--;
return data;
}
};