WAP For Insertion, Deletion and Displaying in A Circular Queue
WAP For Insertion, Deletion and Displaying in A Circular Queue
WAP For Insertion, Deletion and Displaying in A Circular Queue
#include<iostream.h>
#include<conio.h>
#include<process.h>
int ins_cq(int [],int);
void disp(int[], int, int);
int del_cq(int cque[]);
const int size=7;
int cque[size], front =-1, rear=-1;
void main()
{
int it, res;
int ch;
do
{
clrscr();
cout<<"\t\t\t Circular queue Menu\n";
cout<<"\t\t\t--------------------------\n\n\n";
cout<<"\t 1. Insert\n";
cout<<"\t 2. Delete\n";
cout<<"\t 3. Display\n";
cout<<"\t 4. Exit\n\n\n";
cout<<"Enter your choice....... : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter item for insertion : ";
cin>>it;
res=ins_cq(cque,it);
if (res==-1)
cout<<"Overflow\n";
else
{
cout<<"\nNow the Circular Queue is : \n";
disp(cque,front,rear);
}
getch();
break;
case 2: it=del_cq(cque);
cout<<"Element deleted is : "<<it<<"\n";
disp(cque,front,rear);
getch();
break;
case 3: disp(cque,front,rear);
getch();
break;
case 4: break;
default : cout<<"You have entered invalid choice....";
getch();
break;
}
} while(ch!=4);
}
//-------------------------------------------------------------------------int ins_cq(int cque[],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 ++;
cque[rear]=ele;
return 0;
}
//-------------------------------------------------------------------------void disp(int cque[],int front,int rear)
{
int i=0;
cout<<"\nCircular Queue is : \n"
<<"(Front shown as >>>, rear as <<< and free space as #)\n";
if(front ==-1) return;
if(rear>=front)
{
for(i=0;i<front;i++)
cout<<" # ";
cout<<">>>";
for(i=front; i<rear;i++)
cout<<cque[i]<<"<-";
cout<<cque[rear]<<"<<<"<<"\n";
}
else
{
for(i=0;i<rear;i++)
cout<<cque[i]<<"<-";
cout<<cque[rear]<<"<<<";
for(;i<front;i++)
cout<<" # ";
cout<<">>>";
for(i=front;i<size;i++)
cout<<cque[i]<<"<-";
cout<<"\t.......wrap around.......";
}
}
//--------------------------------------------------------------------------int del_cq(int cque[])
{
int ret;
if(front==-1)
return -1;
else
{
ret = cque[front];
if(front==rear)
front=rear=-1;
else if (front ==size-1)
front=0;
else
front++;
}
return ret;
}
OUTPUT-: