PROGRAM TO IMPLEMENT SEARCH IN CIRCULAR QUEUE

/*PROGRAM TO IMPLEMENT SEARCH OPERATION IN CIRCULAR QUEUE*/
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#define MAX 5
void main()
{
int front=-1,rear=-1,i,a[MAX],item,c=0;
char ch;
clrscr();
cout<<“Inserting elements in the queue:”<<endl;
do
{
if((front==0 && rear==MAX-1)||(front==rear+1))
{
cout<<“!!Queue is full!!”<<endl;
break;
}
else if(front==-1 && rear==-1)
{
rear=0;
front=0;
cout<<“Enter the element you want to insert: “;
cin>>item;
a[rear]=item;
}
else if(rear==MAX-1  && front!=0)
{
rear=0;
cout<<“Enter the element you want to insert: “;
cin>>item;
a[rear]=item;
}
else
{
rear++;
cout<<“Enter the element you want to insert: “;
cin>>item;
a[rear]=item;
}
cout<<“Do you want to insert more elements?? “;
cin>>ch;
}while(ch==’y’);
cout<<“Elements in the queue are: “<<endl;
for(i=front;i<=rear;i++)
{
cout<<a[i]<<“\t”;
}
cout<<endl<<“Enter the element you want to search: “;
cin>>item;
for(i=front;i<=rear;i++)
{

if(item==a[i])
{
cout<<“Element found at “<<i+1<<” position”<<endl;
c++;
}
}
if(c==0)
cout<<“!!Element is not present in the queue!!”<<endl;
else
cout<<“Element found “<<c<<” times in the queue”<<endl;

getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *