Go语言用map实现堆栈功能的方法
更新时间:2015年02月28日 10:02:33 作者:小萝莉
这篇文章主要介绍了Go语言用map实现堆栈功能的方法,实例分析了Go语言使用map操作堆栈的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Go语言用map实现堆栈功能的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
package stack
import (
"strconv"
)
type Stack struct {
quenu map[int]int
}
func New() *Stack{
s := new(Stack)
s.quenu = make(map[int]int)
return s
}
func (s *Stack) Push(i int) {
s.quenu[len(s.quenu)] = i
}
func (s *Stack) Pop() {
delete(s.quenu, len(s.quenu)-1)
}
func (s *Stack) String() string {
info := ""
for i := 0; i < len(s.quenu); i++ {
info = info + "[" + strconv.Itoa(i) + "," + strconv.Itoa(s.quenu[i]) + "]"
}
return info
}
import (
"strconv"
)
type Stack struct {
quenu map[int]int
}
func New() *Stack{
s := new(Stack)
s.quenu = make(map[int]int)
return s
}
func (s *Stack) Push(i int) {
s.quenu[len(s.quenu)] = i
}
func (s *Stack) Pop() {
delete(s.quenu, len(s.quenu)-1)
}
func (s *Stack) String() string {
info := ""
for i := 0; i < len(s.quenu); i++ {
info = info + "[" + strconv.Itoa(i) + "," + strconv.Itoa(s.quenu[i]) + "]"
}
return info
}
希望本文所述对大家的Go语言程序设计有所帮助。
相关文章
go语言中切片Slice与数组Array对比以及panic: runtime error: index out
go语言中数组与其他语言有在显著的不同,包括其不能够进行添加,以及值拷贝的特性,下面这篇文章主要给大家介绍了关于go语言中切片Slice与数组Array对比以及panic: runtime error: index out of range问题解决的相关资料,需要的朋友可以参考下2022-07-07
最新评论