无序链表顺序查找 golang

2017-01-03  本文已影响0人  博林木木
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
}
上一篇下一篇

猜你喜欢

热点阅读