package main import ( "fmt" ) //定义栈结构 type Stack struct { StrArray []string Size int Top int } func NewStack(size int) *Stack { return &Stack{StrArray: make([]string, size), Size: size, Top: -1} } //push数据 func (arr *Stack) Push(item string) bool { if arr.IsFull() { return false } arr.Top++ arr.StrArray[arr.Top] = item return true } //取出数据 func (arr *Stack) Pop() (string, bool) { if arr.IsEmpty() { return "", false } item := arr.StrArray[arr.Top] arr.Top-- return item, true } //判断是否空栈 func (arr *Stack) IsEmpty() bool { return arr.Top == -1 } //判断是否满栈 func (arr *Stack) IsFull() bool { return arr.Top == arr.Size - 1 } func main() { stack := NewStack(5) stack.Push("a") stack.Push("b") stack.Push("c") fmt.Print(stack.StrArray) fmt.Print(stack.Top) }