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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路