C语言中的指针
这是我的一些代码。我有个问题 包括“binarytree.h”C语言中的指针,c,pointers,C,Pointers,这是我的一些代码。我有个问题 包括“binarytree.h” intinsert2(intnum,二进制树**根) { 如果(*root==NULL) { *root=setnode(num,NULL); 如果(*root==NULL) 返回1; 其他的 返回0; } 二叉树*temp,*p; 温度=*根; while(temp!=NULL) { //p=温度; 如果(numkey) { 温度=温度->左侧; if(temp==NULL) printf(“temp=NULL”);
intinsert2(intnum,二进制树**根)
{
如果(*root==NULL)
{
*root=setnode(num,NULL);
如果(*root==NULL)
返回1;
其他的
返回0;
}
二叉树*temp,*p;
温度=*根;
while(temp!=NULL)
{
//p=温度;
如果(numkey)
{
温度=温度->左侧;
if(temp==NULL)
printf(“temp=NULL”);
}
否则如果(数值>温度->键)
温度=温度->右侧;
}
如果(numkey)
temp->left=setnode(num,temp);
else temp->right=setnode(num,temp);
返回0;
}
我不知道为什么在一些迭代之后会打印“temp=NULL”,但在while temp!=空值是真的!?怎么办
while ( temp != NULL )
{
//p = temp;
if ( num < temp->key )
{
temp = temp->left;
if ( temp == NULL )
printf( "temp = NULL");
}
else if ( num > temp->key)
temp = temp->right;
}
while(温度!=NULL)
{
//p=温度;
如果(numkey)
{
温度=温度->左侧;
if(temp==NULL)
printf(“temp=NULL”);
}
否则如果(数值>温度->键)
温度=温度->右侧;
}
因为您在while循环中更改了temp
。请记住,while
表达式在每次迭代之前都要进行测试
我们只能从您的代码中假定
temp->left
为空 请改进标题,使其不那么含糊。还请注意,您可以在while
和printf
temp=temp->left之间更改temp
的值代码>在打印为空之前发生。。。这就是它被设置为NULL的地方。在它打印“temp=NULL”之前,您将当前temp节点移动到while循环执行比较时测试的节点的左侧子节点。您在该行之前更改temp
的值。。。但是while()
条件在下一次循环迭代之前不会再次检查。
while ( temp != NULL )
{
//p = temp;
if ( num < temp->key )
{
temp = temp->left;
if ( temp == NULL )
printf( "temp = NULL");
}
else if ( num > temp->key)
temp = temp->right;
}