3 Data structures | Practice | GeeksforGeeks

Given N operations. Each of type 1 or type 2 or type 3
Type 1: "1 x", insert number x in the priority queue (Greatest element on the top)
Type 2: "2 x", insert number x in the queue (First In First Out)
Type 3: "3 x", insert number x in the stack (First In Last Out)

The task is to print the elements in all three data structures from top to bottom. First print elements of the priority queue, then elements of the queue and then elements of the stack.
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains a single integer N.
3. The next N lines contains any operation as mentioned above. 

Output: For each test case, print the elements as mentioned above

Constraints:
1. 1 <= T <= 100
2. 1 <= N <= 104
3. 1 <= X <= 109

Example:
Input:
2
5
1 10
2 5
1 5
2 4
3 1
1
3 100
Output:
10 5 5 4 1
100
0
msbritogabriella2 months ago
Easy C++ Solution

#include <bits/stdc++.h>
#define fim "\n";
#define ll long long;
using namespace std;

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin >> t;
    while(t--){
        int n;
        priority_queue<int> pq;
        queue<int> q;
        stack<int> s;
        cin >> n;
        while(n--){
            int op, val;
            cin >> op >> val;
            if (op == 1) pq.push(val);
            if (op == 2) q.push(val);
            if (op == 3) s.push(val);
        }
        while(!pq.empty()){
            cout << pq.top() << " ";
            pq.pop();
            }
            while(!q.empty()){
                cout << q.front() << " ";
                q.pop();
            }
            while(!s.empty()){
                cout << s.top() << " ";
                s.pop();
            }
        cout << fim;
    }
    return 0;
}
+2
badgujarsachin835 months ago
#include<stack>
using namespace std;
int main()
 {
	//code
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    priority_queue<int > pq;
	    stack<int> st;
	    queue<int> q;
	    while(n--){
	        int type,value;
	        cin>>type;
	        cin>>value;
	        if(type==1){
	            pq.push(value);
	        }
	        else if(type==2){
	            q.push(value);
	        }
	        else if(type==3){
	            st.push(value);
	        }
	    }
	    while(!pq.empty()){
	        cout<<pq.top()<<" ";
	        pq.pop();
	    }
	    while(!q.empty()){
	        cout<<q.front()<<" ";
	        q.pop();
	    }
	    while(!st.empty()){
	        cout<<st.top()<<" ";
	        st.pop();
	    }
	    cout<<endl;
	}
	
	return 0;
}
-1
Laxman Kumar vashist9 months ago
Laxman Kumar vashist
https://uploads.disquscdn.c...
-1
Aman Kumar9 months ago
Aman Kumar
#include<iostream>#include<stack>#include<queue>using namespace std;int main() {   int t; cin>>t; while(t--){     int n;     cin>>n;     priority_queue<int> pq;     queue<int> q;     stack<int> st;     while(n--){         int type,x;         cin>>type>>x;         if(type==1){             pq.push(x);         }        else if(type==2){             q.push(x);         }        else if(type==3){             st.push(x);         }     }     while(!pq.empty()){         cout<<pq.top()<<" ";="" pq.pop();="" }="" while(!q.empty()){="" cout<<q.front()<<"="" ";="" q.pop();="" }="" while(!st.empty()){="" cout<<st.top()<<"="" ";="" st.pop();="" }="" cout<<"\n";="" }="" return="" 0;="" }="">
-1
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.10
#include <bits stdc++.h="">using namespace std;int main(){    ios_base::sync_with_stdio(false);     cin.tie(NULL);        int T;    cin >> T;        while(T--)    {        int N;        cin >> N;                priority_queue<int> pq;        queue<int> q;        stack<int> st;                while(N--)        {            int type, x;            cin >> type >> x;                        if(type == 1)            {                pq.push(x);            }            else if(type == 2)            {                q.push(x);            }            else if(type == 3)            {                st.push(x);            }        }                while(!pq.empty())        {            cout << pq.top() << " ";                        pq.pop();        }                while(!q.empty())        {            cout << q.front() << " ";                        q.pop();        }                while(!st.empty())        {            cout << st.top() << " ";                        st.pop();        }                cout << "\n";    }        return 0;}
-1
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:2.79
import java.util.*;import java.lang.*;import java.io.*;class GFG {    public static void main (String[] args)    {        Scanner sc = new Scanner(System.in);                int T = sc.nextInt();                while(T-- > 0)        {            int N = sc.nextInt();                        PriorityQueue<integer> pq = new PriorityQueue<>(Collections.reverseOrder());            ArrayDeque<integer> q = new ArrayDeque<>();            ArrayDeque<integer> st = new ArrayDeque<>();                        while(N-- > 0)            {                int type = sc.nextInt();                int x = sc.nextInt();;                                if(type == 1)                {                    pq.offer(x);                }                else if(type == 2)                {                    q.offer(x);                }                else if(type == 3)                {                    st.push(x);                }            }                        while(!pq.isEmpty())            {                System.out.print(pq.poll() + " ");            }                        while(!q.isEmpty())            {                System.out.print(q.poll() + " ");            }                        while(!st.isEmpty())            {                System.out.print(st.poll() + " ");            }                        System.out.println();        }    }}
-1
Aaditya Burujwale1 year ago
Aaditya Burujwale
Java Solution
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.