-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.go
More file actions
58 lines (49 loc) · 1021 Bytes
/
Stack.go
File metadata and controls
58 lines (49 loc) · 1021 Bytes
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
package main
import (
//"fmt"
"log"
)
// 用数组实现stack几乎是性能最优的方式
type Stack []interface{}
func (s Stack) IsEmpty() bool {
return len(s) == 0
}
func (s Stack) Size() int {
return len(s)
}
func (s *Stack) Push(item interface{}) {
*s = append(*s, item)
}
func (s *Stack) Pop() interface{} {
n := len(*s) - 1
result := (*s)[n]
*s = (*s)[:n]
return result
}
func (s *Stack) Top() interface{} {
if s.Size() <= 0 {
log.Fatal("null stack")
}
return (*s)[s.Size()-1 ]
}
//func main() {
// stack := new(Stack)
// fmt.Println(stack.IsEmpty())
// //stack.Push("a")
// //stack.Push("a")
// //stack.Push("a")
// //stack.Push("b")
// //stack.Push("b")
// //stack.Push("c")
// //stack.Push("d")
// //stack.Push("e")
// fmt.Println(cap(*stack))
// fmt.Println(stack.Top())
// //fmt.Println(stack.Pop())
// //fmt.Println(stack.Pop())
// //fmt.Println(stack.Pop())
// //fmt.Println(stack.Pop())
// //fmt.Println(stack.Pop())
// //fmt.Println(stack.Pop())
// //fmt.Println(stack.Size())
//}