问题描述
- 这个程序麻烦各位大神帮忙看下错在哪里了,谢谢
-
#include
#include
#include
typedef int ElemType;
typedef struct Student
{
int num;
char name[20];
char birthday[20];
char tel[20];
}Student;
typedef struct BiTNode
{
struct Student stu;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
int SearchBST(BiTree T,int num,BiTree f,BiTree *p,char name[])
{
if(!T)
{
*p = f;
return 0;
}
else
{
if(!strcmp(name,T->stu.name))
{
*p = T;
return 1;
}
else
{
if(num < T->stu.num)
{
return SearchBST(T->lchild,num,T,p,name);
}
else
{
return SearchBST(T->rchild,num,T,p,name);
}
}
}
}
void ProOrderTraverse(BiTree T)
{
if(T==NULL)
{
return;
}
else
{
printf("name: ");
printf("num: ");
printf("birthday ");
printf("tel ");
printf("n");
printf("%-10s",T->stu.name);
printf("%-10d",T->stu.num);
printf("%-10s",T->stu.birthday);
printf("%-10s",T->stu.tel);
printf("n");
ProOrderTraverse(T->lchild);
ProOrderTraverse(T->rchild);
}
}
int InsertBST(BiTree *T,int num,char name[],char birthday[],char tel[])
{
BiTree p,s;
if(!SearchBST(*T,num,NULL,&p,name))
{
s = (BiTree)malloc(sizeof(BiTNode));
s->stu.num= num;
strcpy(s->stu.name,name);
strcpy(s->stu.birthday,birthday);
strcpy(s->stu.tel,tel);
s->lchild = s->rchild =NULL;
if(!p)
{
*T = s;
}
else
{
if(num<(p->stu.num))
{
p->lchild = s;
}
else
{
p->rchild = s;
}
}
return 1;
}
else
{
return 0;
}
}
void InsertStudent(BiTree *T)
{
int num;
char name[20];
char birthday[20];
char tel[20];
printf("Insert name:n");
scanf(" %s",&name);
printf("Insert num:n");
scanf(" %d",&num);
printf("Insert birthday:n");
scanf(" %s",&birthday);
printf("Insert tel:n");
scanf(" %s",&tel);
InsertBST(T,num,name,birthday,tel);
}
int Delete(BiTree *T,char name[10])
{
if(!*T)
{
return 0;
}
else
{
if(!strcmp(name,(*T)->stu.name))
{
return Delete(*T);
}
else
{
return Delete(&(*T)->lchild,name);
return Delete(&(*T)->rchild,name);
}
}
}
int Delete(BiTree *p)
{
BiTree q,s;
if((*p)->rchild == NULL)
{
q = *p;
*p = (*p)->lchild;
free(q);
}
else
{
if((*p)->lchild == NULL)
{
q = *p;
*p = (*p)->rchild;
free(q);
}
else
{
q=*p;
s=(*p)->lchild;
while(s->rchild)
{
q = s;
s = s->rchild;
}
(*p)->stu.num=s->stu.num;
strcpy((*p)->stu.name,s->stu.name);
strcpy((*p)->stu.birthday,s->stu.birthday);
strcpy((*p)->stu.tel,s->stu.tel);
if(q != *p)
{
q->rchild = s->lchild;
}
else
{
q->lchild = s->lchild;
}
free(s);
}
}
return 1;
}
void DeleteStudent(BiTree *T)
{
char name[20];
printf("Delete the name:n");
scanf(" %s",&name);
Delete(T,name);
}
int Alter(BiTree *T,char name[10])
{
if(!*T)
{
return 0;
}
else
{
if(!strcmp(name,(*T)->stu.name))
{
return Alter(*T);
}
else
{
return Alter(&(*T)->lchild,name);
return Alter(&(*T)->rchild,name);
}
}
}
int Alter(BiTree *p)
{
int num;
char name[20];
char birthday[20];
char tel[20];
printf("name: ");
printf("num: ");
printf("birthday ");
printf("tel ");
printf("n");
printf("%-10s",(*p)->stu.name);
printf("%-10d",(*p)->stu.num);
printf("%-10s",(*p)->stu.birthday);
printf("%-10s",(*p)->stu.tel);
printf("n");
printf("Alter the name to:n");
scanf(" %s",&name);
printf("Alter the num to:n");
scanf(" %d",&num);
printf("Alter the birthday to:n");
scanf(" %s",&birthday);
printf("Alter the tel to:n");
scanf(" %s",&tel);
(*p)->stu.num=num;
strcpy((*p)->stu.name,name);
strcpy((*p)->stu.birthday,birthday);
strcpy((*p)->stu.tel,tel);
return 1;
}
void AlterStudent(BiTree *T)
{
char name[20];
printf("Alter the name:n");
scanf(" %s",&name);
Alter(T,name);
}
int main()
{
int i;
BiTree T=NULL;
while(1)
{
printf("1.Show studentn");
printf("2.Insert studentn");
printf("3.Delete studentn");
printf("4.Alter studentn");
do
{
scanf(" %d",&i);
}
while(i!=1&&i!=2&&i!=3&&i!=4);
system("clear");
switch(i)
{
case 1:
{
ProOrderTraverse(T);
break;
}
case 2:
{
ProOrderTraverse(T);
InsertStudent(&T);
break;
}
case 3:
{
ProOrderTraverse(T);
DeleteStudent(&T);
ProOrderTraverse(T);
break;
}
case 4:
{
ProOrderTraverse(T);
AlterStudent(&T);
break;
}
default:
{
break;
}
}
}
return 0;
}
解决方案
不是我疯了,那一定就是你疯了.
解决方案二:
调试程序,如果程序蹦了,点重试,看看断在哪一行,看看内存中变量的数据是否正常
解决方案三:
程序还没看 不知道你include的是什么
解决方案四:
程序还没看 不知道你include的是什么
解决方案五:
程序还没看 不知道你include的是什么
解决方案六:
程序还没看 不知道你include的是什么
时间: 2024-08-17 20:14:24