Advertisement

QUEUE


# include<iostream>
using namespace std;
# define SIZE 5
int a[SIZE];
int front =0;
int rear =0;
void insert(int i)
{
if(rear >= SIZE)
cout<<"\nQUEUE IS FULL\n";
else
a[rear++] = i;
}
void remove()
{
if(front == rear)
cout<<"\nQUEUE IS EMPTY\n ";
else
{
for(int i=0;i<rear;i++)
a[i]=a[i+1];
rear--;
}
}
void display()
{
cout<<"\n";
for(int i=front;i<rear;i++)
cout<<"\t"<<a[i];
}
int main()
{
int option;
char choice;
cout<<"Choose any of the following option "<<endl<<endl;
do
{
cout<<"\n1.INSERTION";
cout<<"\n2.DELETION";
cout<<"\n3.EXIT";
cout<<"\n\nENTER YOUR CHOICE : ";
cin>>option;
switch(option)
case 1:
int n;
cout<<"How many elements you want to insert ";
cin>>n;
cout<<"\nENTER "<<n<<" elements in queue : ";
for(int x =0; x<n; x++)
{
int num;
cin>>num;
insert(num);
}
cout<<"Elements inserted in the queue are "<<endl;
display();
break;
case 2:
int n1;
cout<<"\nHow many elements you want to remove from the queue? :>> ";
cin>>n1;
for(int y = 0; y<n1; y++)
{
remove();
}
cout<<"\nQueue after removal of "<<n1<<" elements"<<endl;
display();
break;
case 3:
exit(0);
default:
cout<<"Invalid choice...";
}
cout<<"\n\nDo you want to repeat the program? Enter Y/N: ";
cin>>choice;
}while(choice == 'y' || choice == 'Y');
return 0;

}

OUTPUT