0% found this document useful (0 votes)
41 views4 pages

Circular Queue Implementation in C++

The document discusses circular queue data structure and its implementation in C++. It defines a circular queue as a special queue where the last element is connected to the first element, forming a circle. It then shows the code implementation of basic circular queue operations like insertion, deletion, and display. The main function tests these operations by performing sample insertions and deletions on the circular queue object and displaying the queue after each step.

Uploaded by

Malik Khawar
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)
41 views4 pages

Circular Queue Implementation in C++

The document discusses circular queue data structure and its implementation in C++. It defines a circular queue as a special queue where the last element is connected to the first element, forming a circle. It then shows the code implementation of basic circular queue operations like insertion, deletion, and display. The main function tests these operations by performing sample insertions and deletions on the circular queue object and displaying the queue after each step.

Uploaded by

Malik Khawar
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

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