-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_reverse.cpp
More file actions
39 lines (37 loc) · 559 Bytes
/
queue_reverse.cpp
File metadata and controls
39 lines (37 loc) · 559 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
*Queue_reverse
*2018年7月13日 20:25:01
*将队列中元素逐一取出并依次压入某一辅助栈,然后再逐一取出栈中的元素并依次插回原队列;
*/
#include<iostream>
#include<stack>
#include<queue>
template<typename T>
void queue_reverse(std::queue<T> &t){
std::stack<T> s;
while(!t.empty())
{
s.push(t.front());
t.pop();
}
while(!s.empty())
{
t.push(s.top());
s.pop();
}
}
int main(){
std::queue<int> t;
int tmp;
while(std::cin>>tmp)
{
t.push(tmp);
}
queue_reverse(t);
while(!t.empty())
{
std::cout<<t.front();
t.pop();
}
return 0;
}