Implementation of Circular Queue
#include <iostream>
#define MAX_SIZE 5
using namespace std;
class Queue {
public:
int front, rear;
int arr[MAX_SIZE];
Queue() { front = rear = 0; }
bool isEmpty()
{
if (front == rear)
return true;
return false;
}
bool isFull()
{
if ((rear + 1) % MAX_SIZE == front)
return true;
return false;
}
void enqueue(int val)
{
if (this->isFull()) {
printf("Queue Overflow!\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
arr[rear] = val;
}
void dequeue()
{
if (this->isEmpty()) {
printf("Queue Underflow!\n");
return;
}
front = (front + 1) % MAX_SIZE;
}
int peek()
{
if (this->isEmpty()) {
printf("Queue is Empty!\n");
return -1;
}
return arr[(front + 1) % MAX_SIZE];
}
void print()
{
if (this->isEmpty())
return;
for (int i = (front + 1) % MAX_SIZE; i < rear;
i = (i + 1) % MAX_SIZE) {
printf("%d ", arr[i]);
}
cout << arr[rear];
}
};
int main()
{
Queue q;
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.dequeue();
q.dequeue();
q.enqueue(123);
q.print();
return 0;
}