Insert and Delete operations on a Static Queue :-
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int size =5;
int top=-1,end=-1,a[size];
void push()
{
int n;
if(end==(size-1))
{
cout<<"\nOVERFLOW";
return;
}
cout<<"\nEnter an element to add in queue : ";
cin>>n;
if(top==-1)
{
top=0;
}
end++;
a[end]=n;
cout<<"\nElement Inserted ";
}
void pop()
{
if(top==-1)
{
cout<<"\nUNDERFLOW";
return;
}
else if(top==end)
{
top=end=-1;
}
for(int i=0;i<=end;i++)
{
a[i]=a[i+1];
}
if(end>-1)
{ end--; }
}
void show()
{
if(top==-1)
{
cout<<"\nQueue is Empty ";
return;
}
cout<<"\nStatus of queue is :- \n";
for(int i=top;i<=end;i++)
{
cout<<" <- "<<a[i];
}
}
void main()
{
int c;
while(1)
{
cout<<"\n\nPress 1 - to enter element\nPress 2- to delete element\nPress 4 - to exit: \n";
cin>>c;
switch(c)
{
case 1:
push();
show();
break;
case 2:
pop();
show();
break;
case 4:
exit(0);
default:
cout<<"\nINVALID OPTION !!\nChoose valid option from given menu....";
break;
}
}
}
No comments:
Post a Comment