```
package main
import "fmt"
/* 栈结构*/
type Stackstruct {
val []interface{}
length uint32
}
/* 构造一个空栈*/
func (S *Stack) InitStack() {
S.val = make([]interface{},0)
S.length =0
}
/* 销毁栈*/
func (S *Stack) DestroyStack() {
S.ClearStack()
}
/* 清空栈*/
func (S *Stack) ClearStack() {
S.InitStack()
}
/* 判断栈是否为空,空的话,返回true,不空返回false */
func (S *Stack) StackEmpty() bool {
if S.length <=0 {
return true
}else {
return false
}
}
/* 返回栈的长度*/
func (S *Stack) StackLength() uint32 {
return S.length
}
/* 获取栈顶元素*/
func (S *Stack) GetTop()interface{} {
return S.val[0]
}
/* 插入一个元素到栈中*/
func (S *Stack) Push(einterface{}) {
S.val = append(S.val, e)
S.length +=1
}
/* 出栈*/
func (S *Stack) Pop()interface{} {
if S.length ==0 {
return nil
}
e := S.val[0]
S.length -=1
S.val = S.val[:S.length]
return e
}
/* 从栈低到栈顶依次对S的每个元素调用visit().一旦visit()失败,则栈操作失败*/
func (S *Stack) StackTraverse(visitfunc(a ...interface{})) {
for i := uint32(0);i < S.length;i +=1 {
visit(S.val[i])
}
}
func p(a ...interface{}) {
fmt.Print(a[0]," ")
}
```
网友评论