gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:42:32 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com