这段代码在输出的时候,总是异常,但是本人又没找出异常点,希望大家帮忙找一下.
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;
void CreateBiTree(BiTNode *T);
int PreOrderTraverse(BiTNode *T);
int main(){
BiTNode *T;
CreateBiTree(T);
PreOrderTraverse(T);
}
void CreateBiTree(BiTNode *T){
char value;
printf("请输入结点的值");
scanf("%c",&value);
getchar();
if (value == "0"){
T == NULL;
}else{
T = (BiTNode*)malloc(sizeof(BiTNode));
T->data = value;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int PreOrderTraverse(BiTNode *T){
if (T == NULL){
return 0;
}else{
printf("%c",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
解决方案
40
1.CreateBiTree函数应该传二级指针或一级指针的引用才行
2.第23行T == NULL;应该是*T = NULL;
2.第23行T == NULL;应该是*T = NULL;
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
void CreateBiTree(BiTNode **T);
int PreOrderTraverse(BiTNode *T);
int main(){
BiTNode *T;
CreateBiTree(&T);
PreOrderTraverse(T);
}
void CreateBiTree(BiTNode **T){
char value;
printf("请输入结点的值");
scanf("%c", &value);
getchar();
if (value == "0"){
*T = NULL;
}
else{
*T = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->data = value;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
int PreOrderTraverse(BiTNode *T){
if (T == NULL){
return 0;
}
else{
printf("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}