输入的的时候我把%c改成%s才能输入请问这是怎么回事啊
关于二叉树的操作下面是我的程序关于二叉树的建立,插入,删除,前序。中序。后续,打印。
问题出现在主函数
请往下看 问题在主函数的地方有标出
#include<stdio.h>
#include<malloc.h>
typedef char datatype;
typedef struct btreenode
{
datatype data;
struct btreenode *lchild;
struct btreenode *rchild;
}nodetype;
/*nodetype *initiate(nodetype *bt)
{ if((bt=(nodetype *)malloc(sizeof(nodetype)))==NULL) return 0;
bt->lchild=NULL;
bt->rchild=NULL;
return bt;
}*/
int initiate(nodetype **bt) /*初始化*/
{ if((*bt=(nodetype *)malloc(sizeof(nodetype)))==NULL) return 0;
(*bt)->lchild=NULL;
(*bt)->rchild=NULL;
return 1;
}
nodetype *create(nodetype *bt, datatype x) /*创建二叉树*/
{ bt->data=x;
return bt;
}
void preorder(nodetype *bt) /*前序遍历二叉树*/
{ if(bt==NULL)return;
printf("%20c%20p%20p%20p\n",bt->data,bt,bt->lchild,bt->rchild);
preorder(bt->lchild);
preorder(bt->rchild);
}
void InOrder(nodetype *bt)//中序遍历二叉树
{
if (bt==NULL) return;
InOrder(bt->lchild);
printf("%20c%20p%20p%20p\n",bt->data,bt,bt->lchild,bt->rchild);
InOrder(bt->rchild);
}
void PostOrder(nodetype *bt)//后序遍历二叉树
{
if (bt==NULL) return;
{
PostOrder(bt->lchild);
PostOrder(bt->rchild);
printf("%20c%20p%20p%20p\n",bt->data,bt,bt->lchild,bt->rchild);/*访问根结点*/
}
}
nodetype *search(nodetype *bt, datatype x) /*查找数据*/
{ nodetype *p;
if(bt==NULL) return NULL;
if(bt->data==x) return bt;
if(bt->lchild!=NULL)
{ p=search(bt->lchild,x);
if(p!=NULL)return p; }
if(bt->rchild!=NULL)
{ p=search(bt->rchild,x);
if(p!=NULL)return p; }
return NULL;
}
nodetype *insertl(datatype x,nodetype *parent) /*将x插入到结点parent的左孩子处*/
{ nodetype *p;
if(parent==NULL)
{ printf("\n error");
return NULL; }
if((p=(nodetype *)malloc(sizeof(nodetype)))==NULL) return NULL;
p->data=x;
p->lchild=NULL;
p->rchild=NULL;
if(parent->lchild==NULL)parent->lchild=p;
else
{ p->lchild=parent->lchild;
parent->lchild=p; }
return p;
}
nodetype *insertr(char x,nodetype *parent) /*将结点x插入到结点parent的右孩子结点*/
{ nodetype *p;
if(parent==NULL)
{ printf("\n error");
return NULL; }
if((p=(nodetype *)malloc(sizeof(nodetype)))==NULL) return NULL;
p->data=x;
p->lchild=NULL;
p->rchild=NULL;
if(parent->rchild==NULL)parent->rchild=p;
else
{ p->rchild=parent->rchild;
parent->rchild=p; }
return p;
}
nodetype *insert(nodetype *bt)
{ char x,y,z;nodetype *parent,*p;
printf("输入您要插入的字符.\n");
scanf("%s",&x);
printf("请选择您要插入的结点的双亲.\n");
scanf("%s",&y);
parent=search(bt,y);
printf("left(l) or right(r).\n");
scanf("%s",&z);
if(z=='l'){
if((p=insertl(x,parent))==NULL)
{ printf("\n内存不够");
return 0; }
}
if(z=='r'){
if((p=insertr(x,parent))==NULL)
{ printf("\n内存不够");
return 0; }
}
return p;
}
nodetype *deleted(nodetype *bt) /*删除*/
{ nodetype *parent,*p;char x,y;
printf("输入删除字符的双亲结点.\n");
scanf("%s",&y);
parent=search(bt,y);
printf("left or right.\n");
scanf("%c",&x);
if(x=='l'){
p=parent->lchild;
parent->lchild=NULL;
free(p);
}
if(x=='r')
{p=parent->rchild;
parent->rchild=NULL;
free(p); }
return parent;
}
void printtree(nodetype *bt,int n) /*屏幕显示树*/
{ int i;
if(bt==NULL) return ;
printtree(bt->rchild,n+1);
for(i=0;i<n-1;++i) printf(" ");
if(n>=1) printf("- - -");
//printf("%s\n",bt->data);
printf("%20c\n",bt->data);
printtree(bt->lchild,n+1);
}
void printftree(nodetype *bt)
{ if(bt==NULL) return;
printf("%-20c%-20p%-20p%-20p\n",bt->data,bt,bt->lchild,bt->rchild);
printftree(bt->lchild);
printftree(bt->rchild);
}
void main ()
{ int m ,n=1;char i,j;
while(1)
{
printf("1==创建一个二叉树\n2==插入一个结点\n3==删除一个结点\n4==屏幕显示数的存储结构\n5==前序遍历二叉树\n6==中序遍历二叉树\n7==后序遍历二叉树\n8==查找一个元素\n9==退出\n");
scanf("%d",&m);
if(m==9)
break;
switch(m)
{
case 1:
nodetype *bt;
bt=(nodetype *)malloc(sizeof(nodetype));
initiate( &bt);
printf("结点数据");
scanf("%s",&i); //注意这里,我吧%s改成%c 结果显示的时候就直接跳过了这行,可是我定义的I是字符类型的啊这是怎么回事啊?????
create( bt, i);
printftree( bt);
break;
case 2:
insert( bt);
n=n+1;
printftree( bt);
break;
case 3:
deleted( bt);
printftree( bt);
break;
case 4:
printtree(bt, n);
break;
case 5:
preorder( bt);
break;
case 6:
InOrder( bt);
break;
case 7:
PostOrder( bt);
break;
case 8:
printf("输入查找数据的数据");
scanf("%s",&j );
search( bt, j);
//printftree( bt);
break;
default :
printf("输入错误");
}
}
}