73 lines
1.0 KiB
Go
73 lines
1.0 KiB
Go
package _4_tree
|
|
|
|
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
|
|
}
|
|
|
|
if this.top > len(this.data)-1 {
|
|
this.data = append(this.data, v)
|
|
} else {
|
|
this.data[this.top] = v
|
|
}
|
|
}
|
|
|
|
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])
|
|
}
|
|
}
|
|
}
|