The Go language USES map to implement the stack method

  • 2020-05-19 05:00:15
  • OfStack

This article demonstrates an example of how the Go language USES map to implement stack functionality. Share with you for your reference. The specific implementation method is as follows:

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
}

I hope this article has been helpful to your programming of Go language.


Related articles: