Computer >> Máy Tính >  >> Lập trình >> C ++

Chương trình C ++ để triển khai Priority_queue trong STL

Hàng đợi ưu tiên là một loại bộ điều hợp vùng chứa, trong đó phần tử đầu tiên của hàng đợi là phần tử lớn nhất trong tất cả các phần tử trong hàng đợi. Các phần tử cũng có thứ tự không giảm trong hàng đợi ưu tiên. Phần tử có mức độ ưu tiên cao được phân phát trước phần tử có mức độ ưu tiên thấp, trong hàng đợi ưu tiên.

Chức năng và mô tả:

Functions used here:
   pq.size() = Returns the size of priority queue.
   pq.insert) = It is used to insert elements to the priority queue.
   pq.delete() = Deletes the value from the priority queue.
   pq.top() = Returns a reference to the top most element of priority queue.

Mã mẫu

#include<iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
   priority_queue<int> pq;
   int c, i;
   while (1) {
      cout<<"1.Size of the Priority Queue"<<endl;
      cout<<"2.Insert Element into the Priority Queue"<<endl;
      cout<<"3.Delete Element from the Priority Queue"<<endl;
      cout<<"4.Top Element of the Priority Queue"<<endl;
      cout<<"5.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of the Queue: ";
            cout<<pq.size()<<endl;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            pq.push(i);
         break;
         case 3:
            i = pq.top();
            if (!pq.empty()) {
               pq.pop();
               cout<<i<<" Deleted"<<endl;
            } else {
               cout<<"Priority Queue is Empty"<<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;
}

Đầu ra

1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Size of the Queue: 0
Enter value to be inserted: 2
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Size of the Queue: 1
Enter value to be inserted: 2
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 3
2 Deleted
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Size of the Queue: 1
Enter value to be inserted: 2
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 3
2 Deleted
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 2
Enter value to be inserted: 7
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 2
Enter value to be inserted: 6
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 4
Top Element of the Queue: 7
1.Size of the Priority Queue
2.Insert Element into the Priority Queue
3.Delete Element from the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 5
Exit code: 1