A heap data structure implemented in go

The heap implemented by golang mainly provides two methods, push and pop and the size of the heap. The code is as follows:

package main

import (
    "errors"
    "fmt"
)

type Stack []interface{}

func (s *Stack) Push(x interface{}) {
    *s = append(*s, x)
}

func (s *Stack) Pop() (interface{}, error) {
    if len(*s) == 0 {
        return nil, errors.New("slice为空!")
    }
    result := (*s)[len(*s)-1]
    *s = (*s)[:len(*s)-1]
    return result, nil
}

func (s *Stack) Len() int {
    return len(*s)
}

func main() {
    s := new(Stack)
    s.Push(1)
    s.Push(2)
    fmt.Println(s, s.Len())
    s.Pop()
    fmt.Println(s, s.Len())
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523421&siteId=291194637