-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_reverse.cpp
More file actions
39 lines (37 loc) · 556 Bytes
/
stack_reverse.cpp
File metadata and controls
39 lines (37 loc) · 556 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
/*
*Stack_reverse
*2018年7月13日 20:01:33
*将栈中元素逐一取出并依次插入某一辅助队列,然后再逐一取出队列中的元素并依次插回原栈;
*/
#include<iostream>
#include<stack>
#include<queue>
template<typename T>
void stack_reverse(std::stack<T> &s){
std::queue<T> t;
while(!s.empty())
{
t.push(s.top());
s.pop();
}
while(!t.empty())
{
s.push(t.front());
t.pop();
}
}
int main(){
std::stack<int> s;
int tmp;
while(std::cin>>tmp)
{
s.push(tmp);
}
stack_reverse(s);
while(!s.empty())
{
std::cout<<s.top();
s.pop();
}
return 0;
}