Queue is a linear data structure.
In queue the elements enter into the queue at one side and
deleted from another end. The main operations performed in the queue are inserting and deleting.
Queue operations
#include<iostream.h>
#include<conio.h>
template<class T>
class queue
T arr[50];
int rear,front;
public:
queue():rear(-1),front(-1)
void insert(T n)
if(rear>=50)
cout<<”Overflow”;
else
rear=rear+1;
arr[rear]=n;
if(front==-1)
front=0;
}
T del()
if(front<0)
cout<<”Underflow”;
return 0;
else
T temp;
temp=arr[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
return temp;
};
void main()
int j,ch,ch1,ch2;
char k;
float g;
clrscr();
queue<int>oi;
queue<char>oc;
queue<float>of;
cout<<”Enter the type of data\n 1. Int\n 2. Char\n 3. Float\n Enter your choice:”;
cin>>ch;
clrscr();
do
switch(ch)
case 1:cout<<”\n 1. Insert\n 2. Delete \n Enter your choice:”;
cin>>ch1;
switch(ch1)
case 1: cout<<”Enter the element:”;
cin>>j;
oi.insert(j);
break;
case 2:
j=oi.del();
cout<<endl<<”Element:”<<j;
getch();
break;
}
clrscr();
break;
case 2: cout<<”\n 1. Insert\n 2. Delete \n Enter your choice:”;
cin>>ch1;
switch(ch1)
case 1: cout<<”Enter the element:”;
cin>>k;
oc.insert(k);
break;
case 2:
k=oc.del();
cout<<endl<<”Element:<<k”;
getch();
break;
clrscr();
break;
case 3: cout<<”\n 1. Insert\n 2. Delete \n Enter your choice:”;
cin>>ch1;
switch(ch1)
case 1: cout<<”Enter the element:”;
cin>>g;
of.insert(g);
break;
case 2:
g=of.del();
cout<<endl<<”Element:<<g”;
getch();
break;
clrscr();
break;
cout<<”Do you want to continue(y/n):\n 1. Yes\n 2. No\n Enter your choice”;
cin>>ch2;
while(ch2==1);
getch();
The output for the above program is given below:
Enter the type of data
1. Int
2. Char
3. Float
Enter your choice:
1. Insert
2. Delete
Enter your choice
Enter the element:
Do you want to continue
1. Yes
2. No
No