PROGRAM IMPLEMENT SEARCH OPERATION IN A QUEUE USING ARRAY

/*PROGRAM TO IMPLEMENT SEARCH OPERATION IN A QUEUE USING ARRAY*/
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#define MAX 5
void main()
{
int i,a[MAX],rear=-1,front=-1,n,num,count=0;
char ch;
clrscr();
do
{
if(rear>=MAX-1)
{
cout<<“Queue is full”<<endl;
break;
}
else
{
rear++;
cout<<“Enter the number to be inserted: “;
cin>>n;
a[rear]=n;
}
cout<<“Do you want to insert more numbers: “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<“Elements of queue are: “<<endl;
for(i=0;i<=rear;i++)
{
cout<<a[i]<<“\t”;
}
//SEARCHING IN THE QUEUE
cout<<endl<<“Enter the element to be searched: “;
cin>>n;
for(i=front;i<=rear;i++)
{
if(n==a[i])
{
cout<<“Element found at “<<i+1<<” position”<<endl;
count++;
}
}
if(count==0)
cout<<“Element is not present in the queue”;
else
cout<<“Element is present at “<<count<<” places”<<endl;
getch();
}

Leave a Reply

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