-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraystack.go
More file actions
71 lines (59 loc) · 1.13 KB
/
Copy patharraystack.go
File metadata and controls
71 lines (59 loc) · 1.13 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
package stack
import (
"fmt"
)
type ArrayStack struct {
data []interface{}
top uint //栈顶下一项. 空栈,top=0
}
var capacity uint = 100
func NewArrayStack() *ArrayStack {
return &ArrayStack{
data: make([]interface{}, 0, capacity),
top: uint(0),//栈顶索引
}
}
func (this *ArrayStack) Pop() interface{} {
if this.IsEmpty() {
return nil
}
value := this.data[this.top-1]
this.top--
return value
}
func (this *ArrayStack) Push(v interface{}) {
//如果满了,返回false或者扩容
if this.top >= uint(cap(this.data)) {
return
}
if uint(len(this.data)) > this.top {
//覆盖之前的值
this.data[this.top] = v
} else {
//新加item
this.data = append(this.data, v)
}
this.top++
}
func (this *ArrayStack) Top() interface{} {
if this.IsEmpty() {
return nil
}
return this.data[this.top - 1]
}
func (this *ArrayStack) IsEmpty() bool {
return this.top == uint(0)
}
func (this *ArrayStack) Flush() bool {
this.data = this.data[:0]
this.top = 0
return true
}
func (this *ArrayStack) Print() {
if this.IsEmpty() {
return
}
for i := uint(0); i < this.top; i++ {
fmt.Println(this.data[i])
}
}