# session1_stackAndQueue
# 题目&&思路code03-递归和栈操作逆序一个栈
使用递归来完成栈的逆序
思路
- 封装一个获取栈底的函数
- 封装逆序
# 代码
package session
import (
"fmt"
"io"
"testing"
)
type Code3Stack struct {
data []int
}
func (c *Code3Stack) Push(value int) {
c.data = append(c.data, value)
}
func (c *Code3Stack) Show() []int {
return c.data
}
func (c *Code3Stack) Pop() (int, error) {
if len(c.data) == 0 {
return 0, io.EOF
}
res := c.data[len(c.data)-1]
c.data = c.data[:len(c.data)-1]
return res, nil
}
func (c *Code3Stack) IsEmpty() bool {
return len(c.data) == 0
}
// =>
func Code3PopLast(stack *Code3Stack) (int, error) {
if stack.IsEmpty() {
return 0, io.EOF
}
value, _ := stack.Pop()
if stack.IsEmpty() {
return value, nil
} else {
last, _ := Code3PopLast(stack)
stack.Push(value)
return last, nil
}
}
func Code3Re(stack *Code3Stack) {
if stack.IsEmpty() {
return
}
value, _ := Code3PopLast(stack)
Code3Re(stack)
stack.Push(value)
}
func TestCode3(t *testing.T) {
stack := Code3Stack{data: []int{1, 2, 3}}
fmt.Println(stack.Show())
Code3Re(&stack)
fmt.Println(stack.Show())
}
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
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