You are on page 1of 4

DATA STRUCTURE AND ALGORITHM

Submitted To:

Mr. Wasif Ali


Submitted By:

Khawar Khurshid
Roll No:

21014198-156
Department:

BS-SE-C
Course code:

SE-209
Circular Queue
A Circular Queue is a special version of queue where the last element of the queue is connected
to the first element of the queue forming a circle. The operations are performed based on FIFO
(First in First Out) principle. It is also called 'Ring Buffer.

Here is the implementation of the basic operations for circular queue in C++:

Code
#include<iostream>
using namespace std;
class CQueue
{
int CQ[5],front,rear,n,counter;
public:
CQueue()
{
front=-1;
rear=-1;
n=5;
counter=0;
}
void Insertion(int data)
{
if(counter==n)
{
cout<<"Circular Quene is full."<<endl;
}
else
{
rear=(rear+1)%n;
CQ[rear]=data;
if(front==-1)
{
front=0;
}
counter++;
}
}
void Deletion()
{
if(counter==0)
{
cout<<"Circular Quene is empty."<<endl;
}
else if(front==rear)
{
rear=-1;
front=-1;
}
else
{
front=(front+1)%n;
counter--;
}
}
void Show()
{
for(int i=0;i<=counter-1;i++)
{
cout<<CQ[(front+i)%n]<<" ";
}
cout<<endl;
}
};
int main()
{
CQueue W;
W.Insertion(10);
cout<<"Queue after first insertion\t";
W.Show();
W.Insertion(20);
cout<<"Queue after Second Insertion\t";
W.Show();
W.Deletion();
cout<<"Queue after Deletion\t";
W.Show();
W.Insertion(30);
cout<<"Queue after last Insertion\t";
W.Show();
return 0;
}

You might also like