-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.go
52 lines (44 loc) · 1009 Bytes
/
Stack.go
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
package kit
// Stack 存放interface{}的栈通用定义
type Stack struct {
elements []interface{}
}
// NewStack 返回*Stack
func NewStack() *Stack {
// []interface{}初始化需要make
// []int{}不需要
return &Stack{elements: make([]interface{}, 0)}
}
// Len 返回栈的长度
func (s *Stack) Len() int {
return len(s.elements)
}
// Push 将x放入栈
func (s *Stack) Push(x interface{}) {
s.elements = append(s.elements, x)
}
// Pop 弹出栈顶部 element
func (s *Stack) Pop() interface{} {
x := s.elements[s.Len()-1]
s.elements = s.elements[:s.Len()-1]
return x
}
// Peek 查看栈顶部 element,并不修改内部结构
func (s *Stack) Peek() interface{} {
x := s.elements[s.Len()-1]
return x
}
// Seek 从栈底部依次取出 element, 不修改内部结构
func (s *Stack) Seek(i int) interface{} {
if i < 0 {
i = 0
}
if i > s.Len()-1 {
i = s.Len() - 1
}
return s.elements[i]
}
// IsEmpty 返回s是否为空
func (s *Stack) IsEmpty() bool {
return s.Len() == 0
}