由于在 golang 中没有方便的 interface 转 string 使用了
"github.com/spf13/cast"
包
type Stack struct { arraryList.ArraryList } func main() { stack := InitNoParam() for i := 0; i < 10; i++ { stack.Push(i) } stack.Pop() fmt.Println(stack.ToString()) } func InitNoParam() *Stack { return &Stack{arraryList.ArraryList{Slice: make([]interface{}, 0)}} } func Init(cap int) *Stack { return &Stack{arraryList.ArraryList{Slice: make([]interface{}, 0, cap)}} } func (this *Stack) Push(elem interface{}) { this.ArraryList.AddLast(&this.Slice, elem) } func (this *Stack) Pop() { this.Slice = this.Remove(this.Slice, len(this.Slice)-1) } func (this *Stack) Peek() (interface{}) { return this.GetLast(this.Slice) } func (this *Stack) GetSize() (int) { return len(this.Slice) } func (this *Stack) IsEmpty() (bool) { if len(this.Slice) == 0 { return true } return false } func (this *Stack) ToString() string { s := "[" for index, sl := range this.Slice { if index == len(this.Slice)-1 { s += cast.ToString(sl)+"] top" } else { s += cast.ToString(sl) + "," } } return s } func (this *Stack) GetCapacity() (interface{}) { return cap(this.Slice) }
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于