Queue and Priority Queue
To Implement queue and priority queue in c++Queue and Priority Queue program
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int> q;
priority_queue<int> pq;
int x;
cout<<"Queue"<<endl;
do
{
cout<<"Enter positive integer (-1 to
stop) : ";
cin>>x;
if(x!=-1) q.push(x);
}while(x!=-1);
cout<<"First Element : "<<q.front()<<endl;
cout<<"Last Element : "<<q.back()<<endl;
cout<<"Priority Queue"<<endl;
do
{
cout<<"Enter positive integer (-1 to
stop) : ";
cin>>x;
if(x!=-1) pq.push(x);
}while(x!=-1);
while(!pq.empty())
{
x=pq.top();
pq.pop();
cout<<x<<" ";
}
cout<<endl;
return 0;
}
OUTPUT:
Queue
Enter positive integer (-1 to stop) : 5
Enter positive integer (-1 to stop) : 6
Enter positive integer (-1 to stop) : 1
Enter positive integer (-1 to stop) : 7
Enter positive integer (-1 to stop) : 3
Enter positive integer (-1 to stop) : 8
Enter positive integer (-1 to stop) : -1
First Element : 5
Last Element : 8
Priority Queue
Enter positive integer (-1 to stop) : 1
Enter positive integer (-1 to stop) : 2
Enter positive integer (-1 to stop) : 3
Enter positive integer (-1 to stop) : 4
Enter positive integer (-1 to stop) : 5
Enter positive integer (-1 to stop) : -1
5 4 3 2 1
Tags:
queue and priority queue