algo/go/08_stack/StackBasedOnArray.go

73 lines
1.0 KiB
Go
Raw Normal View History

2018-10-09 23:54:01 +08:00
package _8_stack
import "fmt"
/*
基于数组实现的栈
*/
type ArrayStack struct {
//数据
data []interface{}
//栈顶指针
top int
}
func NewArrayStack() *ArrayStack {
return &ArrayStack{
data: make([]interface{}, 0, 32),
top: -1,
}
}
func (this *ArrayStack) IsEmpty() bool {
if this.top < 0 {
return true
}
return false
}
func (this *ArrayStack) Push(v interface{}) {
if this.top < 0 {
this.top = 0
} else {
this.top += 1
}
2018-10-10 23:01:59 +08:00
if this.top > len(this.data)-1 {
this.data = append(this.data, v)
} else {
this.data[this.top] = v
}
2018-10-09 23:54:01 +08:00
}
func (this *ArrayStack) Pop() interface{} {
if this.IsEmpty() {
return nil
}
v := this.data[this.top]
this.top -= 1
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])
}
}
}