drnzpd579935 2015-02-25 08:47
浏览 58
已采纳

Golang中LinkedList <T>的等效项是什么

In my use case, I would like to know how the following Java code would be implemented in Go

class TreeNode {
    public int data;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(){}
}

LinkedList<TreeNode> treeList = new LinkedList<TreeNode>();

I am able to import the container/list package and add an interface. But it is not allowing any generic object. Do I have to implement my own version of list with TreeNode struct?

I just need to know how LinkedList<T> would work in Go.

EDIT 1: To make it clear, I am adding the complete code here. I am trying to find the linked list of all nodes at each depth in a binary tree. I used two packages list and binary tree. You can find the source code for binarytree here and list here. list is same as container/list but I added few extra functions

package main
import (
    "fmt"
    "go/chapter02-linkedlists/list"
    "go/chapter04-treesandgraphs/binarytree"
)
func main() {

    inArr := []int{4, 5, 7, 8, 9}
    t1 := binarytree.NewMinimalHeightBST(inArr, 0, len(inArr)-1)
    binarytree.InOrderTraverse(t1)
    var nodeList []*list.List

    nodeList = getLevelbasedList(t1, 0)

    fmt.Println()
    for _, value := range nodeList {
        fmt.Print("[ ")
        for x := value.Front(); x != nil; x = x.Next() {
            fmt.Print(x.Value.(int), " ")
        }
        fmt.Println("]")
    }
}

func getLevelbasedList(root *binarytree.Tree, level int) []*list.List {
    if root == nil {
        return nil
    }
    var nodeList []*list.List
    parents := list.New()
    current := list.New()

    current.PushFront(root)

    for current.Len() > 0 {
        nodeList = append(nodeList, current)
        parents = current
        current = list.New()

        for x := current.Front(); x != nil; x = x.Next() {
            node := x.Value.(*binarytree.Tree)
            if node.Left != nil {
                current = current.PushFront(node.Left)
            }
            if node.Right != nil {
                current = current.PushFront(node.Right)
            }
        }
        return nodeList
    }
}

And the error is,

./question4_4b.go:56: cannot use current.PushFront((interface {})(node.Left)) (type *list.Element) as type *list.List in assignment
./question4_4b.go:59: cannot use current.PushFront((interface {})(node.Right)) (type *list.Element) as type *list.List in assignment

EDIT 2: Based on JamesHenstridge's comment I edited from

current = current.PushFront(node.Left)

to

current.PushFront(node.Left)

And the issue resolved. But now I am getting interface conversion error,

[ panic: interface conversion: interface is *binarytree.Tree, not int

goroutine 1 [running]:
  • 写回答

1条回答 默认 最新

  • dpql57753 2015-02-25 09:04
    关注

    Go doesn't support generic types (see FAQ question Why does Go not have generic types?).

    You have to use Type assertions to obtain the typed value you want.

    E.g. create your TreeNode type:

    type TreeNode struct {
        Data  int
        Left  *TreeNode
        Right *TreeNode
    }
    

    And to iterate over a list containing TreeNode values:

    l := list.New()
    // Populate list
    
    for e := l.Front(); e != nil; e = e.Next() {
        if tn, ok := e.Value.(TreeNode); ok {
            // do something with tn which is of type TreeNode
            fmt.Println(tn)
        } else {
            // e.Value is not of type TreeNode
        }
    }
    

    If you assemble the list and you can be sure it only contains values of type TreeNode, you can omit the error check in the type assertion and it becomes like this:

    for e := l.Front(); e != nil; e = e.Next() {
        // if e.Value would not be of type TreeNode, run-time panic would occur
        tn := e.Value.(TreeNode) // tn is of type TreeNode
        fmt.Println(tn)
    
    }
    

    Edit:

    The error you're getting:

    cannot use current.PushFront((interface {})(node.Left)) (type *list.Element)
        as type *list.List in assignment
    

    At line:

    current = current.PushFront(node.Left)
    

    The current variable is of type list.List, and the method current.PushFront() returns a value of type *list.Element. These are 2 different types, you can't assign a *Element to a variable that has a type of List.

    Edit 2:

    Your 2nd error:

    panic: interface conversion: interface is *binarytree.Tree, not int
    

    Is caused by the line:

    fmt.Print(x.Value.(int), " ")
    

    You try to assert that the value x.Value is of type int but it isn't! x.Value is of type *binarytree.Tree so the assertion will obviously fail.

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

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。