gpt4 book ai didi

对于 c 中的给定代码,无法将节点**转换为节点**错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:43 26 4
gpt4 key购买 nike

对于给定的代码,我收到“无法将节点**转换为节点**错误”。这里都是节点**。将函数 insertintoBST 与其声明进行比较时出错。节点是一个结构变量。在这里,我尝试实现了对二叉搜索树的插入。

#include<stdio.h>
#include<conio.h>

void inserttoBST(struct node**,int,int);

void main()
{
int choice,i,j;

struct node{
int value;
int key;
struct node*left;
struct node*right;
};

struct node*root=NULL;

do{
printf("\n1.insert\n2.delete\n3.view list\n4.search");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("\nEnter the key and data :");
scanf("%d%d",&i,&j);
inserttoBST(&root,i,j);
break;
case 2: break;
case 3:break;
case 4:break;
default:printf("\nInvalid Entry");
}
}while(choice!=10);
}


void inserttoBST(struct node**root,int keys,int val)
{
if(*root==NULL)
{
struct node*nodes=(struct node*)malloc(sizeof(struct node));
nodes->key=key;
nodes->val=val;
nodes->left=nodes->right=NULL;
*root=nodes;
}
else if((*root)->key<keys)
inserttoBST((*root)->right,int keys,int val);
else inserttoBST((*root)->left,int keys,int val);
}

最佳答案

您的第一个问题是 struct node 没有在您使用它的第一点声明。

您需要将定义从 main 移到 inserttoBST 原型(prototype)之前。

这是代码存在的许多问题中的第一个,您还需要查看:

  • 去掉 conio.h,它不是标准的头文件。
  • 包括 stdlib,因为 malloc 需要它。
  • 确定您的键变量应该是 key 还是 keys
  • 确定您的值结构字段应该是 val 还是 value
  • 从对 inserttoBST调用 中删除 int 类型说明符。
  • 将正确的值作为第一个参数传递给 inserttoBST,特别是 &((*root)->leftOrRight)
  • 对 main 函数使用 int main (void),这是标准特别允许的两个规范变体之一。

这就是我为编译所做的一切,是否足以消除我不能说的任何逻辑错误。

但是,一旦您完成编译,这就是您需要采取的下一步。

关于对于 c 中的给定代码,无法将节点**转换为节点**错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670670/

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