This C++ Program demonstrates implementation of Priority_queue in STL.
Here is source code of the C++ Program to demonstrate Priority_queue in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Implement Priority_queue in Stl
*/
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
priority_queue<int> pq;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Priority Queue Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Priority Queue"<<endl;
cout<<"2.Delete Element from the Priority Queue"<<endl;
cout<<"3.Size of the Priority Queue"<<endl;
cout<<"4.Top Element of the Priority Queue"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
pq.push(item);
break;
case 2:
item = pq.top();
if (!pq.empty())
{
pq.pop();
cout<<"Element "<<item<<" Deleted"<<endl;
}
else
{
cout<<"Priority Queue is Empty"<<endl;
}
break;
case 3:
cout<<"Size of the Queue: ";
cout<<pq.size()<<endl;
break;
case 4:
cout<<"Top Element of the Queue: ";
cout<<pq.top()<<endl;
break;
case 5:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ g++ priority_queue.cpp $ a.out --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 1 Enter value to be inserted: 5 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 1 Enter value to be inserted: 3 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 1 Enter value to be inserted: 7 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 1 Enter value to be inserted: 4 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 1 Enter value to be inserted: 2 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 1 Enter value to be inserted: 8 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 3 Size of the Queue: 6 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 4 Top Element of the Queue: 8 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 2 Element 8 Deleted --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 3 Size of the Queue: 5 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 4 Top Element of the Queue: 7 --------------------- Priority Queue Implementation in Stl --------------------- 1.Insert Element into the Priority Queue 2.Delete Element from the Priority Queue 3.Size of the Priority Queue 4.Top Element of the Priority Queue 5.Exit Enter your Choice: 5 ------------------ (program exited with code: 1) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Data Structure Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Apply for Information Technology Internship
- Buy Data Structure Books