Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Program To Insert, Delete, Display Data in Circular Queue

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

PROGRAM TO INSERT,DELETE,DISPLAY DATA IN CIRCULAR QUEUE

.
#include<iostream.h>
#include<conio.h>

const int size=7;


int cqueue[size],front=-1,rear=-1;

int insertion(int cqueue[],int ele)


{
if((front==0 &&rear==size-1) || (front==rear+1))
return -1;
else if(rear==-1)
front=rear=0;
else if(rear==size-1)
rear=0;
else
rear++;
cqueue[rear]=ele;
return 0;
}

void display(int cqueue[],int front,int rear)


{
int i=0;
cout<<"\nCIRCULAR QUEUE IS :\n";
if(front==-1)
return ;
if(rear>=front)
{
for(i=0;i<front;i++)
cout<<">>>";
for(i=front;i<rear;i++)
cout<<cqueue[i]<<"<--";
cout<<cqueue[rear]<<"<<<"<<endl;
}
else
{
for(i=0;i<rear;i++)
cout<<cqueue[i]<<"<--";
cout<<cqueue[rear]<<"<<<";
for(i=0;i<front;i++)cout<<"-";
cout<<">>>";
for(i=front;i<size;i++)
cout<<"\tRAP AROUND ";
}
}

int deletion(int cqueue[])


{
int ret;
if(front==-1)
return -1;
else
{
ret=cqueue[front];
if(front==rear)
front=rear=-1;
else if(front==size-1)
front=0;
else
front++;
}
return ret;
}

void main()
{
clrscr();
int item,res,ch;
do
{
cout<<"\t\t\tCIRCULAAR QUEUE MENU \n";
cout<<"\t1.INSERT \n";
cout<<"\t2.DELETE \n";
cout<<"\t3.DISPLAY \n";
cout<<"\t4.EXIT \n";
cout<<"ENTER CHOICE(1-4) : ";
cin>>ch;

switch(ch)
{
case 1:clrscr();
cout<<"ENTER ITEM FOR INSERTION :";
cin>>item;
res=insertion(cqueue,item);
if(res==-1)
cout<<"OVERFLOW!!\n";
else
{
cout<<"NOW CIRCULAR QUEUE IS :";
display(cqueue,front,rear);
}
break;

case 2:clrscr();
item=deletion(cqueue);
cout<<"ELEMENT DELETED IS :"<<item<<endl;
display(cqueue,front,rear);
break;

case 3:clrscr();
display(cqueue,front,rear);
break;

case 4:break;

default:cout<<"INVALID CHOICE!!";
break;
}
}
while(ch!=4);
getch();
}

You might also like