二叉树创建问题,为什么输入一个字符后,一直输入‘#’都结束不了
#include <stdio.h>
#include <malloc.h>
#define maxsize 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
} *bitree;
bitree create()
{
bitree t;
char ch;
ch=getchar();
if(ch=='#')
t=NULL;
else
{
t=(struct node *)malloc(sizeof(struct node));
t->data=ch;
t->lchild=create();
t->rchild=create();
}
return t;
}
void preorder(bitree t)
{
if(t==NULL)
return ;
else{
printf("%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
bitree t,p;
t=create();
preorder(t);
printf("...............\n");
}
#include <stdio.h>
#include <malloc.h>
#define maxsize 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
} *bitree;
bitree create()
{
bitree t;
char ch;
scanf("%c",&ch);
printf("%c@\n",ch); //输入的数据 以@标示
if(ch == '#')
{
t=NULL;
printf("!!!!!!!!!!!!!!!!!!!\n");//if执行
}
else
{
t=(struct node *)malloc(sizeof(bitree));
t->data=ch;
printf("...%c\n",t->data);//else执行
t->lchild=create();
t->rchild=create();
}
return t;
}
void preorder(bitree t)
{
if(t == NULL)
return;
else
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
bitree t,p;
t=create();
preorder(t);
printf("...............\n");
}//1 2 3 # # 4 5 # 6 # # 7 # # #
#include <stdio.h>
#include <malloc.h>
#define maxsize 100
typedef struct node
{
char data;
struct node *lchild,*rchild;
} *bitree;
bitree create()
{
bitree t;
char ch;
scanf("%c",&ch);
getchar();
if(ch == '#')
{
t=NULL;
}
else
{
t=(struct node *)malloc(sizeof(bitree));
t->data=ch;
t->lchild=create();
t->rchild=create();
}
return t;
}
void preorder(bitree t)
{
if(t == NULL)
return;
else
{
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
bitree t,p;
t=create();
preorder(t);
printf("...............\n");
}//1 2 3 # # 4 5 # 6 # # 7 # # #