无序链表顺序查找 golang

type node struct {
    next *node
    key int
    value int
}
type st struct {
    head *node
    n int
}

func (this *st) insert(key,value int){
    tail := this.head
    for tail.next != nil{
        tail = tail.next
    }
    tail.next = new(node)
    tail.next.key = key
    tail.next.value = value
    this.n++
}

func (this *st) get(key int) int{
    head := this.head
    for head != nil{
        if head.key == key{
            return head.value
        }
        head = head.next
    }
    return 0
}

func (this *st)delete(key int){
    head := this.head
    if head.key == key{
        this.head = head.next
        return
    }

    for head != nil{
        if head.next.key == key{
            head.next = head.next.next
            return
        }
        head = head.next
    }
    return
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容