-
Notifications
You must be signed in to change notification settings - Fork 140
/
stack.go
68 lines (64 loc) · 1.29 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package stack
import "reflect"
// https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29#Array
// stacks 如同叠盘子, 先进后出
/*
go 语言中就是Array来实现, Slice来实现
last-in, first-out
Motivation, 代码的压栈
IsEmpty() bool
Peek() (interface{}, error)
Push(value interface{})
Pop() ()
*/
type Stack struct {
stack []interface{}
len int
}
func New() *Stack {
s := &Stack{
stack: make([]interface{}, 0),
len: 0,
}
return s
}
func (s *Stack) IsEmpty() bool {
return len(s.stack) == 0
}
func (s *Stack) Peek() (interface{}, bool) {
if len(s.stack) == 0 {
return nil, false
}
return s.stack[0], true
}
func (s *Stack) Push(value interface{}) {
s.stack = append([]interface{}{value}, s.stack...)
s.len++
}
func (s *Stack) Pop() interface{} {
el := s.stack[0]
s.stack = s.stack[1:]
s.len--
return el
}
func (s *Stack) Max() interface{} {
if s.IsEmpty() {
return 0
}
max := s.stack[0]
for i := 0; i < s.len; i++ {
switch v := reflect.ValueOf(s.stack[i]); v.Kind() {
case reflect.String:
if s.stack[i].(string) > max.(string) {
max = s.stack[i]
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if s.stack[i].(int) > max.(int) {
max = s.stack[i]
}
default:
max = 0
}
}
return max
}