doudouba4520 2015-01-16 13:27
浏览 111
已采纳

使用golang从列表中的最后一个元素开始的Kth

I have the code to find the kth from the last element in a list in golang. I wrote a recursive function. When it reaches the end of the list, it will return the count as 1 and increments in further returns. When the count == k then return the node value. But I am getting 'nil pointer dereference' error. Could anyone help me in this?

package main    
import (
    "container/list"
    "fmt"
)

var sMap map[int]bool

func main() {
    l := list.New()
    for i := 1; i < 100; i++ {
        l.PushBack(i)
    }    
    kFromLastElemRec := findKFromLastRecr(l.Front(), 3, WrapObj{0})
    fmt.Println(kFromLastElemRec.Value.(int))    
}
//Object to store the count 
type WrapObj struct {
    count int
}
//ERROR
//recursive function to find the kth from last element
func findKFromLastRecr(head *list.Element, k int, wrapper WrapObj) *list.Element {
    if head == nil {
        return nil
    }

    resNode := findKFromLastRecr(head.Next(), k, wrapper)
    wrapper.count = (wrapper.count) + 1
    if wrapper.count == k {
        return head
    }
    return resNode
}
  • 写回答

1条回答 默认 最新

  • duanchuang1935 2015-01-16 13:55
    关注

    You need to pass a pointer to WrapObj to the findKFromLastRecr() function.

    Similar to the languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter.

    For instance, passing a WrapObj value to a function makes a copy of the WrapObj, but not the WrapObj it points to.

    Therefore, without a pointer to WrapObj, each findKFromLastRecr() function gets a copy of WrapObj and the increase can not be shared by the outer findKFromLastRecr() functions.

    It could be useful to check the pointers section from golang-book.

    package main    
    import (
        "container/list"
        "fmt"
    )
    
    var sMap map[int]bool
    
    func main() {
        l := list.New()
        for i := 1; i < 100; i++ {
            l.PushBack(i)
        }    
        kFromLastElemRec := findKFromLastRecr(l.Front(), 3, &WrapObj{0})
        fmt.Println(kFromLastElemRec.Value.(int))    
    }
    //Object to store the count 
    type WrapObj struct {
        count int
    }
    //ERROR
    //recursive function to find the kth from last element
    func findKFromLastRecr(head *list.Element, k int, wrapper *WrapObj) *list.Element {
        if head == nil {
            return nil
        }
    
        resNode := findKFromLastRecr(head.Next(), k, wrapper)
        wrapper.count = (wrapper.count) + 1
        if wrapper.count == k {
            return head
        }
        return resNode
    }
    

    Output

    97
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记