C++ STL Priority Queue | Practice | GeeksforGeeks

Implement different operations on Priority Queue .i.e. adding element, removing element, size of priority queue, print the queue and top element of queue. 
Input:
The first line of input contains an integer T denoting the no of test cases. For each test case, the first line of input contains an integer Q denoting the no of queries followed by Q space separated queries. 
A query can be of the following types:
1 x (Adding x to the priority queue and print the queue)
2 (Removing the element from the top of the priority queue and printing that element )
3 (Get the element at the top of the priority queue)
4 (Get the size of the priority queue)
5 (Print the priority queue) 
Note: In each case return or print -1 if priority queue is empty
Output:
For each test case, the output is according to the query Q performed and if the priority queue is empty the output is -1.
Constraints:
1<=T<=100
1<=Q<=100
Example:
Input:
1
8
1 6
1 1
1 7
4
3
2
5
2
Output:
6
6 1
7 6 1
3
7
7 
6 1 
6
Explanation:
1 6 means adding 6 in the queue and printing that, similarly adding 1 and 7 in the queue and printing the queue i.e. 7 6 1. By 4 it returns the size of the queue i.e 3. With 3 as input, it returns the element at the top i.e 7. With 2 it removes the top element i.e 7 from the queue and prints the element i.e. 7. Having 5 as input, it prints the queue i.e. 6 1 and again 2 remove the element and prints that i.e 6.
+1
avinav26113 months ago
C++ Solution
 
0
abdulmuqsitzargar015 months ago
void addElement(priority_queue<int> & A, int x){  A.push(x); //your code here}
/* remove top element from the priority queue*/void remove_Element(priority_queue<int> &A){ if(!A.empty()){ cout<<A.top()<<endl; A.pop();} else cout<<"-1"<<endl;//your code here}
/*  returns the top element of the priority queue*/int getElement_at_top(priority_queue<int> &A){  if(!A.empty())  return A.top();  else  return -1;//your code here}
/* returns the size ofthe priority queue*/int getSize(priority_queue<int> &A){  if(!A.empty())  return A.size();  else  return -1;//your code here}
/* prints the element in the priority queue*/void print(priority_queue<int> &A){   if (A.empty()) {       cout << -1 <<endl;   } else {       vector<int> temp;       while (!A.empty()) {           temp.push_back(A.top());           cout << A.top() << " ";           A.pop();       }       cout <<endl;       for (auto &itr:temp) {           A.push(itr);       }   } //your code here}
+3
badgujarsachin835 months ago
void addElement(priority_queue<int> & A, int x)
{
    //your code here
    A.push(x);
}

/* remove top element from 
the priority queue*/
void remove_Element(priority_queue<int> &A)
{
    if (A.empty()) {
        cout << -1 << '\n';
    } else {
        cout << A.top() << '\n';
        A.pop();
    }
}

/*  returns the top element 
of the priority queue*/
int getElement_at_top(priority_queue<int> &A)
{
    //your code here
     return A.empty() ? -1 : A.top();
}
/* returns the size of
the priority queue*/
int getSize(priority_queue<int> &A)
{
    //your code herej
     return A.empty() ? -1 : A.size();
}

/* prints the element in 
the priority queue*/
void print(priority_queue<int> &A)
{
     //your code here
    if (A.empty()) {
        cout << -1 << '\n';
    } else {
        vector<int> temp;
        while (!A.empty()) {
            temp.push_back(A.top());
            cout << A.top() << " ";
            A.pop();
        }
        cout << '\n';
        for (auto &itr:temp) {
            A.push(itr);
        }
    }
}
0
rohitpendse1386 months ago
/*add the element in the priority queue*/void addElement(priority_queue<int> &A, int x) {    //your code here    A.push(x);}/* remove top element fromthe priority queue*/void remove_Element(priority_queue<int> &A) {    //your code here    if (A.empty()) {        cout << -1 << '\n';    } else {        cout << A.top() << '\n';        A.pop();    }}/*  returns the top elementof the priority queue*/int getElement_at_top(priority_queue<int> &A) {    //your code here    return A.empty() ? -1 : A.top();}/* returns the size ofthe priority queue*/int getSize(priority_queue<int> &A) {    //your code here    return A.empty() ? -1 : A.size();}/* prints the element inthe priority queue*/void print(priority_queue<int> &A) {    //your code here    if (A.empty()) {        cout << -1 << '\n';    } else {        vector<int> temp;        while (!A.empty()) {            temp.push_back(A.top());            cout << A.top() << " ";            A.pop();        }        cout << '\n';        for (auto &itr:temp) {            A.push(itr);        }    }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.