#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct tree {
struct tree*lchild,*rchild;
char date;
} tree,*ntree;
char ch[100];
int i=0;
int creat(ntree p) {
while(i<=strlen(ch)) {
if(p->date=='#') return 0;
else {
p->date=ch[i];
creat(p->lchild);
creat(p->rchild);
}
i++;
p=(ntree)malloc(sizeof(tree));
}
}
int main() {
gets(ch);
ntree p,head;
p=head=(ntree)malloc(sizeof(tree));
if(p) {
printf("初始化成功");
creat(p);
}
}
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct tree {
struct tree*lchild,*rchild;
char date;
} tree,*ntree;
char ch[100];
int i=0;
int creat(ntree& p) {
//while(i<=strlen(ch)) {
char c = ch[i++];
if (c == '#' || c == '\0') p = NULL; //if(p->date=='#') return 0;
else {
p=(ntree)malloc(sizeof(tree));
p->date=c;
creat(p->lchild);
creat(p->rchild);
}
//i++;
//p=(ntree)malloc(sizeof(tree));
//}
}
void firstprint(ntree p)
{
if(p){
printf("%4c",p->date);
firstprint(p->lchild);
firstprint(p->rchild);
}
}
int main() {
gets(ch);
ntree p,head;
//p=head=(ntree)malloc(sizeof(tree));
//if(p) {
// printf("初始化成功");
creat(p);
//}
firstprint(p);
return 0;
}
报啥错误