arrays - 语法错误 : unexpected name, 期待)

标签 arrays go

我在 GO 中有我的 BST 代码。我不断收到此错误消息。我正在使用记事本,我是初学者。 错误在我的 for 循环中。在 insertList 函数下。

type node struct{
    left *node
    right *node
    val int
    }


func insert(tree *node, element int) *node{
    if tree == nil{
        tree = &node{nil, nil, element}
        } else if element > tree.val{
        tree.right = insert( tree.right, element)
        } else if element < tree.val{
        tree.left = insert( tree.left, element)
        }
    return tree
    }
func insertList(elementList []int) *node{
    if tree == nil{
        for i:=0; i<[]int.len; i++{ 
            tree = insert([i]int)}
        return tree}}

func displayBST(tree *node){
    if ( tree != nil) {
        displayBST( tree.left)
        fmt.Println(tree.val)
        displayBST(tree.right)}}


func main(){

     l := [10]int{100, 3, 3, 200, 5, 8, 5, 200, 0, -4}

     s := l[:]

     insertList(s)

     displayBST(insertList(s))

     fmt.Println()}

最佳答案

这应该能让您克服语法错误:

package main

import "fmt"

type node struct {
    left  *node
    right *node
    val   int
}

func insert(tree *node, element int) *node {
    if tree == nil {
        tree = &node{nil, nil, element}
    } else if element > tree.val {
        tree.right = insert(tree.right, element)
    } else if element < tree.val {
        tree.left = insert(tree.left, element)
    }
    return tree
}

func insertList(tree *node, elementList []int) *node {
    if tree == nil {
        for i := 0; i < len(elementList); i++ {
            tree = insert(tree, elementList[i])
        }
    }
    return tree
}

func displayBST(tree *node) {
    if tree != nil {
        displayBST(tree.left)
        fmt.Println(tree.val)
        displayBST(tree.right)
    }
}

func main() {

    l := [10]int{100, 3, 3, 200, 5, 8, 5, 200, 0, -4}

    s := l[:]

    var t *node

    insertList(t, s)

    displayBST(insertList(t, s))

    fmt.Println()
}

关于arrays - 语法错误 : unexpected name, 期待),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236670/

相关文章:

c++ - 实现 B=f(A),使用 B 和 A 数组并且 B 已经定义

c - 新手C数组关于函数的问题

C++ 将数组分配给彼此; type int* = type int 有效但 int = int*?

javascript - 比较两个嵌套数组和对象以查找差异

database - go rest api 服务器设计良好实践

c++ - 声明对 Ints 数组的引用时,为什么它必须是对 const 指针的引用?

amazon-web-services - 获取 s3 存储桶上的资源 URI

go - Response.Body 返回 "empty"正文

interface - Golang接口(interface)不需要导入?

html - 在 GoLang 中将 Json 数据映射到 Html 模板