Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Circular Queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include<iostream.h>
#include<conio.h>
#include<process.h>
int insert_in_CQ(int[],int);
void display(int[],int,int);
int del_in_CQ(int[]);

const int size=5;
int CQueue[size],front=-1,rear=-1;

void main()
{int item,res,ch;

do{ system("cls");
cout<<"\t\t\tCircular Queue Menu\n";
cout<<"\t1. Insert\n";
cout<<"\t2. Delete\n";
cout<<"\t3. Display\n";
cout<<"\t4. Exit\n";
cout<<"Enter your choice (1-4)...";
cin>>ch;
switch(ch)
{ case 1: cout<<"\nEnter item for insertion : ";
cin>>item;
res=insert_in_CQ(CQueue,item);
if(res==-1)cout<<"\nOverflow!\n";
else
{ cout<<"\nNow the Circular Queue is:\n";
display(CQueue,front,rear);
}
system("pause");
break;
case 2: item=del_in_CQ(CQueue);
cout<<"Element deleted is: "<<item<<endl;
display(CQueue,front,rear);
system("pause");
break;
case 3: display(CQueue,front,rear);
system("pause");
break;
case 4: break;
default:cout<<"Enter a valid option (1-4) \n";
system("pause");
break;
}
}while(ch!=4);
getch();
}

int insert_in_CQ(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<<"\n Circular 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<<"-"<<">>>";
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<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<size;i++)
cout<<CQueue[i]<<"<-";
cout<<"\t...wrap around...";
}
}
int del_in_CQ(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;
}