package stack import ( "fmt" ) type ArrayStack struct { data []interface{} top uint //æ é¡¶ä¸ä¸é¡¹. 空æ ï¼top=0 } var capacity uint = 100 func NewArrayStack() *ArrayStack { return &ArrayStack{ data: make([]interface{}, 0, capacity), top: uint(0),//æ é¡¶ç´¢å¼ } } func (this *ArrayStack) Pop() interface{} { if this.IsEmpty() { return nil } value := this.data[this.top-1] this.top-- return value } func (this *ArrayStack) Push(v interface{}) { //å¦ææ»¡äºï¼è¿åfalseæè æ©å®¹ if this.top >= uint(cap(this.data)) { return } if uint(len(this.data)) > this.top { //è¦çä¹åçå¼ this.data[this.top] = v } else { //æ°å item this.data = append(this.data, v) } this.top++ } func (this *ArrayStack) Top() interface{} { if this.IsEmpty() { return nil } return this.data[this.top - 1] } func (this *ArrayStack) IsEmpty() bool { return this.top == uint(0) } func (this *ArrayStack) Flush() bool { this.data = this.data[:0] this.top = 0 return true } func (this *ArrayStack) Print() { if this.IsEmpty() { return } for i := uint(0); i < this.top; i++ { fmt.Println(this.data[i]) } }