那么这个代码为啥运行时报错
时间: 2024-03-22 22:40:00 浏览: 30
在这段代码中,宏定义 OK 和 ERROR 没有加括号,导致在函数中使用时可能会出现问题。此外,函数 CreateBST 在返回值时使用了分号,这也会导致编译器产生错误。因此,你可以将代码修改如下:
```
#include <stdio.h>
#include <stdlib.h>
typedef int TElemType;
typedef int Status;
#define OK 1
#define ERROR -1
typedef struct BSTNode{
TElemType data;
struct BSTNode *leftchild,*rightchild;
}BSTNode,*BSTree;
Status CreateBST(BSTree &T,int x) {
if(T==NULL) {
T = (BSTree)malloc(sizeof(BSTNode));
T->data=x;
T->leftchild=T->rightchild=NULL;
return OK;
}
if (x < T->data) {
CreateBST(T->leftchild, x);
} else {
CreateBST(T->rightchild, x);
}
return OK;
}
Status print(BSTree T) {
if(T) {
print(T->leftchild);
printf("%d ",T->data);
print(T->rightchild);
}
return OK;
}
int main() {
int a[100];
int n,i;
BSTree T=NULL;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
for(i=0;i<n;i++) {
CreateBST(T,a[i]);
}
print(T);
printf("\n");
return 0;
}
```
这样修改后,代码就可以正常运行了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)