抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

Stack - 翻转

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
40
41
42
43
44
/*************************************************************************
> File Name: stackRecerse.cpp
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年01月06日 星期四 10时15分16秒
************************************************************************/

#include <iostream>
#include <stack>
using namespace std;

void reverseStack(stack<string> &f, string &res) {
// 当然这里 string res 也可以写成另一个 栈 这里这个引用会加点速
if(!f.empty()) {
string t = f.top();
f.pop();
reverseStack(f, res);
res += t + " ";
}
return ;
}

int main() {

stack<string> f, f1;
f.push("a");
f.push("b");
f.push("c");
f.push("d");
f.push("e");
f.push("f");
f1 = f;
while(!f1.empty()) {
cout << f1.top() << " ";
f1.pop();
}
cout << endl;

string res;
reverseStack(f, res);
cout << res;

return 0;
}

评论