baidu_24427837 于 2014.12.11 18:40 提问

#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 student\n");
printf("2.Insert student\n");
printf("3.Delete student\n");
printf("4.Alter student\n");
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;
}

6个回答

happyparrot      2014.12.12 12:54

lx624909677      2014.12.12 13:40

Learning__exe   2014.12.14 13:57

Learning__exe   2014.12.14 13:57

baidu_24427837 为什么？
3 年多之前 回复
Learning__exe   2014.12.14 13:57

baidu_24427837 <stdio.h>
3 年多之前 回复
Learning__exe   2014.12.14 13:57

​国际标准智商测试题 （转载）     智商，就是IQ（Intelligence Quotient的简称)，通俗地可以理解为智力，是指数字、空间、逻辑、词汇、创造、记 忆等能力，它是德国心理学家施特恩在1912年提出的。智商表示人的聪明程度：智商 越高，则表示越聪明。想检验自己的智商是多少吗？这并不困难，以下就是一例国内 较权威的IQ测试题，请在30分钟内完成（30题），之后你就
《SQL入门经典》学习笔记

TXT在线小说管理器