个人电话本
#include <stdio.h>#include <stdlib.h>
#include <string.h>
enum
{
TypeFind,
TypeAdd,
TypeUpdate_Name,
TypeUpdate_Tel,
TypeDelete,
};
struct telbook
{
char name[20];
int tel;
struct telbook *next;
};
typedef struct telbook Tel,*pTel;
void show(pTel head)
{
while(head)
{
printf("%s: %d\n",head->name,head->tel);
head=head->next;
}
}
void empty(pTel *head)
{
pTel pt;
while(*head)
{
pt=(*head)->next;
free(*head);
*head=pt;
}
}
void input(char *name,int *tel,int type)
{
switch(type)
{
case TypeAdd:
printf("[Add] input telbook name: ");
break;
case TypeFind:
printf("[Find] input telbook name: ");
break;
case TypeUpdate_Name:
printf("[Update] input telbook name: ");
break;
case TypeUpdate_Tel:
printf("[Update] input telbook tel: ");
break;
case TypeDelete:
printf("[Delete] input telbook name: ");
break;
default:
break;
}
if(name!=NULL)
{
scanf("%s",name);
}
if(tel!=NULL)
{
if(type!=TypeFind )
{
scanf("%d",tel);
}
}
}
void find(pTel head,char *name)
{
input(name,NULL,TypeFind);
while(head)
{
if(strcmp(head->name,name)==0)
{
printf("%s: %d\n",name,head->tel);
return;
}
head=head->next;
}
printf("error,no %s tel record!\n",name);
}
void update(pTel head,char *name,int *tel)
{
input(name,NULL,TypeUpdate_Name);
while(head)
{
if(strcmp(head->name,name)==0)
{
input(NULL,tel,TypeUpdate_Tel);
head->tel=*tel;
return;
}
head=head->next;
}
printf("error,no %s tel record!\n",name);
}
void delete(pTel *head,char *name)
{
input(name,NULL,TypeDelete);
pTel p=*head;
pTel q=NULL;
int flag=0;
while(p)
{
if(strcmp(p->name,name)==0)
{
if(p==*head)
{
*head=p->next;
}
else
{
q->next=p->next;
}
free(p);
flag=1;
}
q=p;
p=p->next;
}
if(flag==0)
printf("error,no this %s tel record!\n",name);
}
void sort(pTel head)
{
Tel st=*head;;
pTel p,q;
p=head;
while(p)
{
q=p->next;
while(q)
{
if(strcmp(p->name,q->name)>0)
{
st=*p;
strcpy(p->name,p->next->name);
p->tel=q->tel;
strcpy(q->name,st.name);
q->tel=st.tel;
}
q=q->next;
}
p=p->next;
}
}
//头插法
void push_front(pTel *head,char *name,int *tel)
{
pTel pt=NULL;
input(name,tel,TypeAdd);
pt=malloc(sizeof(Tel));
strcpy(pt->name,name);
pt->tel=*tel;
pt->next=*head;
*head=pt;
}
void push_back(pTel *head,char *name,int *tel)
{
pTel q=*head;
pTel pt=NULL;
input(name,tel,TypeAdd);
pt=malloc(sizeof(Tel));
strcpy(pt->name,name);
pt->tel=*tel;
pt->next=NULL;
if(*head==NULL)
{
*head=pt;
}
else
{
while(q->next)q=q->next;
q->next=pt;
}
return;
}
int main(int argc,char *argv[])
{
pTel head=NULL;
char name[20];
int tel;
push_front(&head,name,&tel);
push_front(&head,name,&tel);
push_back(&head,name,&tel);
push_back(&head,name,&tel);
show(head);
find(head,name);
update(head,name,&tel);
show(head);
delete(&head,name);
show(head);
sort(head);
//empty(&head);
//printf("******empty!...\n");
printf("after sort...\n");
show(head);
return 0;
/*
pTel pt=NULL;
pt=malloc(sizeof(Tel));
strcpy(pt->name,"zhangshan");
pt->tel=1111;
pt->next=NULL;
head=pt;
pt=malloc(sizeof(Tel));
strcpy(pt->name,"lisi");
pt->tel=2222;
pt->next=NULL;
head->next=pt;
pt=malloc(sizeof(Tel));
strcpy(pt->name,"wangwu");
pt->tel=3333;
pt->next=NULL;
head->next->next=pt;
*/
}