我的插入代码没有';在c语言中不能正常工作
我在二叉搜索树中插入节点。但它不能正常工作。这是我的代码:我的插入代码没有';在c语言中不能正常工作,c,insert,tree,C,Insert,Tree,我在二叉搜索树中插入节点。但它不能正常工作。这是我的代码: int adding(node * tree,double x,int y) { node *newN; if(!tree) { newN=(node*)malloc(sizeof(node)); newN->data=x; newN->totalval=y; newN->right=NULL; newN-&
int adding(node * tree,double x,int y)
{
node *newN;
if(!tree)
{
newN=(node*)malloc(sizeof(node));
newN->data=x;
newN->totalval=y;
newN->right=NULL;
newN->left=NULL;
tree=newN;
return 1;
}
if(x < tree->data)
{
adding(tree->left,x,y);
}
if(x==tree->data)
{
printf("This data is already existed. Please try again");
return 0;
}
if(x> tree->data)
{
adding(tree->right,x,y);
}
}
int添加(节点*树,双x,int y)
{
节点*newN;
如果(!树)
{
newN=(node*)malloc(sizeof(node));
newN->data=x;
newN->totalval=y;
newN->right=NULL;
newN->left=NULL;
tree=newN;
返回1;
}
if(xdata)
{
添加(树->左、x、y);
}
如果(x==树->数据)
{
printf(“此数据已存在,请重试”);
返回0;
}
如果(x>树->数据)
{
添加(树->右,x,y);
}
}
另:结构节点有数据,左,右。在这里,插入数据和x不一样。x是从用户处获取的,数据是从文件夹中获取的,并插入到不同的函数中 假设
树
为空
。
我们有时会忘记指针是一个数字。唯一额外的是这个数字是内存中某个字节的偏移量,仅此而已
因此,考虑到null(在C中)是(void*)0
,此代码:
if(!tree)
{
newN=(node*)malloc(sizeof(node));
newN->data=x;
newN->totalval=y;
newN->right=NULL;
newN->left=NULL;
tree=newN;
return 1;
}
可以这样写:
if(!tree)
{
//...
(void*)0 = newN;
return 1;
}
您是否意识到您试图为
0
赋值?为了给指针赋值,而不是给它所指向的变量赋值,我们需要做什么?换句话说,函数应该如何传递指针来更改它?(提示:作为指向指针的指针)tree=newN代码>无效。因为树
是局部变量int adding(node*tree,double x,int y){
-->int adding(node**tree,double x,int y){
并且有一个路径不返回值。请更正答案,但尽量不要问修辞性问题,以防OP不知道答案。