美文网首页
无序链表顺序查找 golang

无序链表顺序查找 golang

作者: 博林木木 | 来源:发表于2017-01-03 16:07 被阅读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
    }
    

    相关文章

      网友评论

          本文标题:无序链表顺序查找 golang

          本文链接:https://www.haomeiwen.com/subject/jvknvttx.html