-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseStackWithRecursive.cpp
More file actions
81 lines (73 loc) · 1.77 KB
/
Copy pathReverseStackWithRecursive.cpp
File metadata and controls
81 lines (73 loc) · 1.77 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<stack>
using namespace std;
class ReverseStackWithRecursive
{
public:
//递归栈,每一个栈拥有独立的空间
//ans每次存储栈底元素
//bottomOut的返回值是弹出后的栈底元素,还未入栈
//递归调用reverse,利用递归栈的特性,先进后出,实现栈的逆序
//相当于第二个栈容器为递归栈,将s的栈元素通过递归展开的方式压入递归栈中
//非递归方法是按照一个栈弹出,另一栈压入
void reverse(stack<int>& s)
{
if(s.empty())
{
return ;
}
int ans = bottomOut(s);
reverse(s);
s.push(ans);
}
//将栈底元素一层层的向上返回,最后通过调用者将返回值压入栈中
//实现栈底元素转化为栈头元素,其他元素顺序不变
//利用递归栈,每一层递归都有自己独立的空间,通过每一层的返回,来保存栈底元素
int bottomOut(stack<int>& s)
{
int ans = s.top();
if(s.size()==1)
{
ans = s.top();
s.pop();
return ans;
}
int last = s.top();
s.pop();
ans = bottomOut(s);
s.push(last);
return ans;
}
int bottomOut_One(stack<int>& s)
{
int ans = s.top();
s.pop();
if(s.empty())
{
return ans;
}
int last = ans;
ans = bottomOut_One(s);
s.push(last);
return ans;
}
};
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
ReverseStackWithRecursive r;
r.reverse(s);
while(!s.empty())
{
cout<<s.top()<< " ";
s.pop();
}
return 0;
}