#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *rchild,*lchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree *t){
char ch;
printf("2\n");//为了检查运行过程
scanf("%c",&ch);
printf("3\n");//为了检查运行过程
if(ch!='#'){
*t = (BiTNode*)malloc(sizeof(BiTNode));
if(*t == NULL){
exit(-1);
}
(*t)->data = ch;
printf("1\n");//为了检查运行过程
CreateBiTree(&((*t)->lchild));
CreateBiTree(&((*t)->rchild));
}
else{
*t = NULL;
printf("0\n");//为了检查运行过程
}
}
int main(void){
BiTree t;
CreateBiTree(&t); //abc00de0g00f000
printf("\n");
system("pause");
return 0;
}
CreateBiTree(&t);这个运行的时候输入一个字符它总是会运行两遍
例如输入一个a:
scanf函数中,“%c” 和 “%c ”,选后者,需要有个空格。因为我们输入字符的时候,回车和空格也会被当作字符然后送到缓冲区,从而你的回车就被当作字符进入了下一个程序中。并且,你的BiTree已经是结构指针的定义,所以后来你的t已经代表的是地址,&t就是地址的地址,这是我纠正了一些,如果我的有问题也请指出。谢谢。
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode {
char data;
struct BiTNode* rchild, * lchild;
}BiTNode, * BiTree;
void CreateBiTree(BiTree t) {
t = (BiTNode*)malloc(sizeof(BiTNode));
char ch ;
printf("2\n");//为了检查运行过程
scanf("%c ", &ch);
printf("3\n");//为了检查运行过程
if (ch != '#') {
t->data = ch;
printf("1\n");//为了检查运行过程
CreateBiTree(t->lchild);
CreateBiTree(t->rchild);
}
else {
t = NULL;
printf("0\n");//为了检查运行过程
}
}
int main(void) {
BiTree t=NULL;
CreateBiTree(t); //abc00de0g00f000
printf("\n");
system("pause");
return 0;
}