delete()
{
struct address *info;
char s[80];
printf("enter name:");
gets(s);
info=find(s);
if (info) {
if (start==info){
start=info->next;
if (start)
start->prior=NULL;
else
last=NULL;
}
else{
info->prior->next=info->next;
if(info!=last)
info->prior->next=info->prior;
else
last=info->prior;
}
free(info);
}
}
struct address *find(char *name)
{
struct address *info;
info=start;
while(info){
if(!strcmp(name,info->name)) return info;
info=info->next;
}
printf("name not found\n");
return NULL;
}
void list()
{
struct address *info;
info=start;
printf("\n%-20s%-20s%-10s%-10s%-6s\n",
"姓名","街道","城市","省","邮编");
while(info){
display(info);
info=info->next;
}
printf("\n\n");
}
void display(struct address *info)
{
printf("%-20s%-20s%-10s%-10s%-6s\n",
info->name,info->street,info->city,info->state,info->zip);
}
void search()
{
char name[40];
struct address *info,*find();
printf("enter name to find:");
gets(name);
info=find(name);
if(!info)
printf("not found\n");
else
display(info);
}
void save()
{
struct address *info;
FILE *fp;
if((fp=fopen("mlist","wb"))==NULL){
printf("can not open file\n");
exit(1);
}
printf("\nSaving file\n");
info=start;
while(info){
fwrite(info,sizeof(struct address),1,fp);
info=info->next;
}
fclose(fp);
}
void load()
{
struct address *info,*temp=NULL;
FILE *fp;
if((fp=fopen("mlist","rb"))==NULL){
printf("can not open file\n");
return;
}
while(start){
info=start->next;
free(info);
start=info;
}
printf("\nLoading file\n");
start=(struct address *)malloc(sizeof(struct address));
if(!start){
printf("out of memory!\n");
return;
}
info=start;
while(!feof(fp)){
if(1!=fread(info,sizeof(struct address),1,fp)) break;
info->next=(struct address *)malloc(sizeof(struct address));
if (!info->next){
printf("out of memory\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
}
temp->next=NULL;
last=temp;
start->prior=NULL;
fclose(fp);
}