Circular Queue Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Circular Queue

“A circular queue is a special version of queue where the last element of queue is connected to
the first element of queue forming a circle.”

• The operations are performed based on FIFO (first in first out) principle.

• It is also called ‘Ring Buffer’.

Example:
Traffic light is one of the best-example of circular queue. Each light of traffic light gets ON one
by one after every interval of time.
Need of circular Queue:

• Circular Queues offer a quick and clean way to store FIFO data with a maximum size.
• Conserve memory as we only store up to our capacity.
Advantage of circular Queue over normal Queue:
❖ Circular Queue is memory more efficient than a linear Queue as we can add elements until
complete. Thus, no space is left over. While in a Linear Queue, once a Queue is full, is we
start to Dequeue, the front indexes become vacant, and then they can never be filled.
Disadvantage:
✓ The biggest disadvantage of circular queue is you can only store queue length
elements. If you are using it as Buffer, you are limiting your history depth.
✓ Another disadvantage is it’s hard to tell an empty queue from a full queue
without retaining additional information.

#include <iostream>
using namespace std;
int stack[7];
int f=-1;
int r=-1;
void inQ()
{
int t=0;
cout<<"enter the value : "<<endl;
cin>>t;
if (f==-1&& r==-1)
{
r++;
stack[r]=t;
f=r;
}
else
{
if(r==4)
{
cout<<"Queue is full...."<<endl;
}
else
{
r++;
stack[r]=t;
}
}
};
void deQ ()
{
If (f==-1 && r==-1)
{
cout<<"\n There is no element in the Queue..."<<endl;
}
else
{
cout<<"element deleted: "<<stack[f];
if (f==0 && r==0)
{
f=r=-1;
}
else
{
f++;
if(f>0 && r==4)
{
r++;
}
}
}
};
void dis ()
{
cout<<"The element you entered are given: "<<endl;
for (int i=f; i<=r; i++)
{
cout<<stack[i];
}
};
int main (int argc, char** argv)
{
int f=-1;
int r=-1;
int ch;

cout<<"\n press '1' to enter Element: "<<endl;


cout<<"\n press '2' to delete Element:"<<endl;
cout<<"\n press '3' to display Element:"<<endl;
cout<<"\n press '4' to Exit:"<<endl;
while (1)
{
cin>>ch;
switch(ch)
{
case 1:
inQ ();
break;

case 2:
deQ ();
break;

case 3:
dis ();
break;

case 4:
exit (1);
break;
default:
cout<<"\n *** enter the correct value ***"<<endl;
break;
}
}
return 0;
}

You might also like