Advertisement
momo2345

queue reversal

Aug 28th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. //Initial Template for C++
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. queue<long long int> rev(queue<long long int> q);
  6. int main()
  7. {
  8.     long long int test;
  9.     cin>>test;
  10.     while(test--)
  11.     {
  12.     queue<long long int> q;
  13.     long long int n, var;
  14.     cin>>n;
  15.     while(n--)
  16.     {
  17.         cin>>var;
  18.         q.push(var);
  19.     }
  20.     queue<long long int> a=rev(q);
  21.     while(!a.empty())
  22.     {
  23.         cout<<a.front()<<" ";
  24.         a.pop();
  25.     }
  26.     cout<<endl;
  27.     }
  28. }// } Driver Code Ends
  29.  
  30.  
  31. //function Template for C++
  32.  
  33. queue<long long int> rev(queue<long long int> q)
  34. {
  35.    vector<long long int>v;
  36.    while(!q.empty()){
  37.    v.push_back(q.front());
  38.    q.pop();
  39.        }
  40.     reverse(v.begin(),v.end());
  41.     long long int i=0;
  42.     long long p=v.size();
  43.     while(p--){
  44.         q.push(v[i]);
  45.         i++;
  46.     }
  47.    return q;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement