Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to reverse a queue in C++

1 Answer

0 votes
#include <bits/stdc++.h> 

using namespace std; 
  
void printQueue(queue<int>& q) { 
    while (!q.empty()) { 
        cout << q.front() << " "; 
        q.pop(); 
    } 
} 
  
void reverseQueue(queue<int>& q) { 
    stack<int> Stack; 
    while (!q.empty()) { 
        Stack.push(q.front()); 
        q.pop(); 
    } 
    while (!Stack.empty()) { 
        q.push(Stack.top()); 
        Stack.pop(); 
    } 
} 
  
int main() 
{ 
    queue<int> q; 
    
    q.push(1); 
    q.push(2); 
    q.push(3); 
    q.push(4); 
    q.push(5); 

    reverseQueue(q); 
    printQueue(q); 
} 



/*
run:

5 4 3 2 1 

*/

 



answered Apr 5, 2020 by avibootz

Related questions

1 answer 133 views
1 answer 133 views
1 answer 163 views
1 answer 150 views
3 answers 213 views
213 views asked Apr 4, 2020 by avibootz
1 answer 143 views
...