0% found this document useful (0 votes)
2 views

Implementation of Circular Queue

The document provides a C++ implementation of a circular queue with a maximum size of 5. It includes methods for checking if the queue is empty or full, adding (enqueue) and removing (dequeue) elements, peeking at the front element, and printing the queue's contents. The main function demonstrates the usage of the queue by enqueuing and dequeuing elements, showcasing its functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Implementation of Circular Queue

The document provides a C++ implementation of a circular queue with a maximum size of 5. It includes methods for checking if the queue is empty or full, adding (enqueue) and removing (dequeue) elements, peeking at the front element, and printing the queue's contents. The main function demonstrates the usage of the queue by enqueuing and dequeuing elements, showcasing its functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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;
}

You might also like