用go实现的一个堆得数据结构

用golang实现的堆,主要提供了两个方法,push和pop及堆的大小,代码如下:

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())
}

猜你喜欢

转载自www.cnblogs.com/ontheway1024/p/9016150.html