用二叉排序树建立。建立时未用平衡二叉树。因此,时间性能上会较差。
#include<stdio.h>
#include<stdlib.h>#include<string.h>
typedef struct AdNode
{
char name[20];
char tel_num[11];
struct AdNode *lchild,*rchild;
}AdNode;
AdNode *a=NULL;
void InsertBST(AdNode **root,AdNode *s)
{
if((*root)==NULL) (*root)=s;
else if(strcmp(s->name,(*root)->name)>0) InsertBST(&(*root)->rchild,s);
else InsertBST(&(*root)->lchild,s);
}
AdNode *Add(AdNode *a)
{
AdNode *s;
s=(AdNode *)malloc(sizeof(AdNode));
printf("Please input the contact's name and telephone:\n");
gets(s->name);
gets(s->tel_num);
s->lchild=NULL;
s->rchild=NULL;
InsertBST(&a,s);
return a;
}
AdNode *Search_name(AdNode *root,char p[])
{
if(root==NULL) return NULL;
else if(strcmp(p,root->name)==0)
return root;
else if(strcmp(p,root->name)>0)
return Search_name(root->rchild,p);
else return Search_name(root->lchild,p);
}
AdNode *Search_tel(AdNode *root,char p[])
{
if(root==NULL) return NULL;
else
{
if(strcmp(p,root->tel_num)==0) return root;
else
{
Search_tel(root->lchild,p);
Search_tel(root->rchild,p);
}
}
}
AdNode *Delete(AdNode *root,char s[])
{
AdNode *pre=NULL,*p=NULL,*d=root,*f=NULL; //f指向双亲节点,d指向待删除节点
while(d!=NULL) //查询待删除节点的位置
{
if(strcmp(s,d->name)==0) break;
f=d;
if(strcmp(s,d->name)>0) d=d->rchild;
else d=d->lchild;
}
if(d==NULL) {printf("ERROR!\n");return root;}
if((d->lchild==NULL)&&(d->rchild==NULL)) //叶子节点
{
if(f==NULL)
{
free(d);
root=NULL;
return root;
}
if(strcmp(d->name,f->name)>0)
{
f->rchild=NULL;
free(d);
}
else
{
f->lchild=NULL;
free(d);
}
}
else if(d->rchild==NULL) //非终端非根节点
{
if(f==NULL)
{
pre=d;
p=d->lchild;
while(p->rchild!=NULL)
{
pre=p;
p=p->rchild;
}
strcpy(d->name,p->name);
strcpy(d->tel_num,p->tel_num);
if(strcmp(pre->name,p->name)==0) pre->lchild=p->lchild;
else pre->rchild=p->lchild;
free(p);
}
else
{
pre=d->lchild;
p=pre->rchild;
while(pre->rchild!=NULL)
{
pre=p;
p=p->rchild;
}
strcpy(d->name,p->name);
strcpy(d->tel_num,p->tel_num);
free(pre);
}
}
else if(d->lchild==NULL)
{
if(f==NULL)
{
pre=d;
p=d->rchild;
while(p->lchild!=NULL)
{
pre=p;
p=p->lchild;
}
strcpy(d->name,p->name);
strcpy(d->tel_num,p->tel_num);
if(strcmp(pre->name,p->name)==0) pre->rchild=p->rchild;
else pre->lchild=p->rchild;
free(p);
}
else
{
pre=d->rchild;
p=pre->lchild;
while(pre->lchild)
{
pre=p;
p=p->lchild;
}
strcpy(d->name,p->name);
strcpy(d->tel_num,p->tel_num);
free(pre);
}
}
else //根节点
{
pre=d;
p=d->rchild;
while(p->lchild)
{
pre=p;
p=p->lchild;
}
strcpy(d->name,p->name);
strcpy(d->tel_num,p->tel_num);
if(strcmp(pre->name,p->name)==0) pre->rchild=p->rchild;
else pre->lchild=p->rchild;
free(p);
}
return root;
}
AdNode *Modify(AdNode *root)
{
int m;
char s[20]={0},ch;
AdNode *q,*p;
p=(AdNode *)malloc(sizeof(AdNode));
p->lchild=NULL;
p->rchild=NULL;
printf("Please input the contact's name you want to modify:\n");
gets(s);
q=Search_name(root,s);
if(q!=NULL)
{ strcpy(p->name,q->name);
strcpy(p->tel_num,q->tel_num);
printf("Please modify the information.\n1.name\t2.telephone\n");
scanf("%d",&m);
getchar();
root=Delete(root,q->name);
switch(m)
{
case 1: printf("Input the new name:\t");
gets(p->name);
printf("Do you continue to modify the telephone? y/n\t");
scanf("%c",&ch);
getchar();
if(ch=='n'||ch=='N')
goto END;
case 2: printf("Input the new telephone:\t");
gets(p->tel_num);
break;
default: printf("ERROR!\n");break;
}
END: InsertBST(&root,p);
printf("联系人:\n");
}
else printf("ERROR!\n");
return root;
}
void InOrder(AdNode *root)
{
if(root==NULL) return ;
else
{
InOrder(root->lchild);
puts(root->name);
puts(root->tel_num);
printf("\n");
InOrder(root->rchild);
}
}
int main()
{
int n;
int i;
char p[20]={0},s[20]={0};
AdNode *q;
do
{
printf("\n1.Add new contact\n2.Modify contact info\n3.Search\n4.Delete contact\n5.exit\n");
printf("Please choose the action:\t");
scanf("%d",&n);
getchar();
switch(n)
{
case 1: a=Add(a);
printf("联系人:\n");
InOrder(a);
break;
case 2: a=Modify(a);
InOrder(a);
break;
case 3: printf("Please input the name or the telephone you want to search\n1.name\t2.telephone\t");
scanf("%d",&i);
getchar();
printf("Please input the information:\t");
gets(p);
switch(i)
{
case 1: q=Search_name(a,p);break;
case 2: q=Search_tel(a,p);break;
default: break;
}
if(q==NULL)
{
printf("It doesn't exist!\n");
break;
}
else
{
printf("您查找的联系人信息如下:\n");
puts(q->name);
puts(q->tel_num);
break;
}
case 4: printf("Please input the name you want to delete:\t");
gets(s);
a=Delete(a,s);
if(a==NULL)
{
printf("No person.\n");
break;
}
else
{ printf("联系人:\n");
InOrder(a);
break;
}
case 5: break;
default: printf("ERROR!\n");break;
}
}while(n!=5);
printf("联系人:\n");
InOrder(a);
return 0;
}