0% found this document useful (0 votes)
11 views6 pages

Assigment 3_CircularQueue

The document contains a C++ implementation of a Circular Queue using templates. It includes methods for checking if the queue is empty or full, enqueueing and dequeueing elements, and displaying the queue's contents. The main function allows user interaction to perform various operations on the Circular Queue.

Uploaded by

jobidad431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Assigment 3_CircularQueue

The document contains a C++ implementation of a Circular Queue using templates. It includes methods for checking if the queue is empty or full, enqueueing and dequeueing elements, and displaying the queue's contents. The main function allows user interaction to perform various operations on the Circular Queue.

Uploaded by

jobidad431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream>

using namespace std;

template<class T>

class CircularQueue

int front,rear,size;

int *arr;

public:

CircularQueue(int s)

size=s;

arr= new T[size];

front=rear=-1;

bool isEmpty();

bool isFull();

void enQueue(T);

int deQueue();

void display();

};

template<class T>

bool CircularQueue<T> :: isEmpty()

{
if(front==-1 && rear==-1)

return true;

else

return false;

template<class T>

bool CircularQueue<T> :: isFull()

if((rear+1)%size==front)

return true;

else

return false;

template<class T>

void CircularQueue<T> :: enQueue(T element)

if(isEmpty())

rear=front=0;

arr[rear]=element;

else

{
rear=(rear+1)%size;

arr[rear]=element;

template<class T>

int CircularQueue<T> :: deQueue()

int item;

if(isEmpty())

cout<< "\nQueue is empty !!!"<<endl;

return 0;

else if(front==rear)

item=arr[front];

arr[front]=0;

rear=front=-1;

return item;

else

item=arr[front];

front=(front+1)%size;
return item;

template<class T>

void CircularQueue<T> :: display()

cout<<"\nThe values in the queue are --> ";

for(int i=front;i!=rear;i=((i+1)%size))

cout<<arr[i]<<" ";

cout<<arr[rear];

int main()

int n;

cout<< "Enter size of the queue : ";

cin>>n;

CircularQueue <int>q1(n);

int option,value;

do {

cout<< "\nOperations on Circular Queue ";

cout<< "\n1 : Enqueue()";


cout<< "\n2 : Dequeue()";

cout<< "\n3 : display()";

cout<< "\n4 : Clear Screen";

cout<< "\n5 : Exit";

cout<< "\nEnter your choice : ";

cin>>option;

switch(option)

case 1:

if(q1.isFull())

cout<< "\nQueue is Full !!!"<<endl;

else

cout<< "\nEnqueue operation --> Enter an item to enqueue to the queue : ";

cin>>value;

q1.enQueue(value);

break;

case 2:

cout<< "\nDequeue operation --> Dequeued value : " <<q1.deQueue()<<endl;

break;
case 3:

q1.display();

cout<< "\n";

break;

case 4:

system("cls");

break;

case 5:

exit(0);

default:

cout<< "Enter proper choice ! "<<endl;

}while(option != 5);

return 0;

You might also like