-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackBasedOnArray.go
More file actions
67 lines (58 loc) · 922 Bytes
/
StackBasedOnArray.go
File metadata and controls
67 lines (58 loc) · 922 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
59
60
61
62
63
64
65
66
67
package stack
import (
"fmt"
)
type ArrayStack struct {
data []interface{}
top int
}
func NewArrayStack() *ArrayStack {
return &ArrayStack{
data:make([]interface{}, 0,32),
top:-1,
}
}
func (a *ArrayStack) isEmpty() bool {
if a.top < 0 {
return true
}
return false
}
func (a *ArrayStack) Push(v interface{}) {
if a.top < 0 {
a.top = 0
} else {
a.top+=1
}
if a.top > len(a.data)-1 {
a.data = append(a.data, v)
}else {
a.data[a.top] = v
}
}
func (a *ArrayStack) Pop() interface{} {
if a.isEmpty() {
return nil
}
v := a.data[a.top]
a.top--
return v
}
func (this *ArrayStack) Top() interface{} {
if this.isEmpty() {
return nil
}
return this.data[this.top]
}
func (this *ArrayStack) Flush() {
this.top = -1
}
func (this *ArrayStack) Print() {
if this.isEmpty() {
fmt.Println("empty statck")
} else {
for i := this.top; i >= 0; i-- {
fmt.Println(this.data[i])
}
}
}