Advertisement
momo2345

reverse queue

Aug 28th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. //Initial Template for C++
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long
  6. queue<ll> modifyQueue(queue<ll> q, int k);
  7. int main(){
  8.     ll t;
  9.     cin>>t;
  10.     while(t-->0){
  11.         ll n,k;
  12.         cin>>n>>k;
  13.         queue<ll> q;
  14.         while(n-->0){
  15.             ll a;
  16.             cin>>a;
  17.             q.push(a);
  18.         }
  19.         queue<ll> ans=modifyQueue(q,k);
  20.         while(!ans.empty()){
  21.             int a=ans.front();
  22.             ans.pop();
  23.             cout<<a<<" ";
  24.         }
  25.         cout<<endl;
  26.     }
  27. }// } Driver Code Ends
  28.  
  29.  
  30. //User function Template for C++
  31.  
  32. queue<ll> modifyQueue(queue<ll> q, int k)
  33. {
  34.    
  35.     stack<int>s;
  36.     queue<int>p;
  37.     while(k--)
  38.     {
  39.         s.push(q.front());
  40.         q.pop();
  41.     }
  42.     while(!q.empty()){
  43.         p.push(q.front());
  44.         q.pop();
  45.     }
  46.     while(!s.empty()){
  47.         q.push(s.top());
  48.         s.pop();
  49.     }
  50.     while(!p.empty()){
  51.         q.push(p.front());
  52.          p.pop();
  53.     }
  54.     return q;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement