帮吗修改下加下注释 呵呵 谢了
#include<stdio.h>#include<stdlib.h>
struct address{
char name[20];
char phone[15];
char build[10];
struct address*next;
struct address*prior;
}list_entry;
struct address*start;/*point to first entry in list*/
struct address*last; /*point to last entry*/
void search();
main()
{
char s[80],choice;
struct address*info;
start=0; /*zero length list*/
for(;;){
choice=menu_select();
switch(choice){
case 1:enter();break;
case 2:delete();break;
case 3:list();break;
case 4:search();break;
case 5:save();break;
case 6:load();break;
case 7:exit(0);
}
}
}
menu_select()
{
char s[80];
int c;
printf("1.Enter a name\n");
printf("2.Delete a name\n");
printf("3.List the file\n");
printf("4.Search\n");
printf("5.Save the file\n");
printf("6.Load the file\n");
printf("7.Quit\n");
do{
printf("Enter your choice:");
gets(s);
c=atoi(s);
}while(c<0||c>7);
return c;
}
enter()
{
struct address*info,*dls_store();
for(;;){
info=malloc(sizeof(list_entry));
if(info==0){
printf("\nout of memory");
return;
}
inputs("enter name:",info->name,20);
if(!info->name[0])break;
inputs("enter phone:",info->phone,15);
inputs("enter build:",info->build,10);
start=dls_store(info,start);
}
}
inputs(prompt,s,count)
char*prompt;
char*s;
int count;
{
char p[255];
do{
printf(prompt);
gets(p);
if(strlen(p)>count)printf("\ntoo long\n");
}while(strlen(p)>count);
strcpy(s,p);
}
struct address*dls_store(i,top);
struct address*i;
struct address*top;
{ static stuct address*last=0;
struct address*old,*p;
if(last==0){
i->next=0;
i->prior=0;
last=i;
return i;
}
p=top;
old=0;
while(p){
if(strcmp(p->name,i->name)<0){
old=p;
p=p->next;
}
else{
if(p->prior){
p->prior->next=i;
i->next=p;
p->prior=i;
return i;
}
}
old->next=i;
i->next=0;
last=i;
return start;
}
delete()
{
struct address*info,*find();
char s[80];
printf("enter name:");
gets(s);
if(info){
if(start==info){
start=info->next;
if(start)start->prior=0};
else
last=0;
}
else{
info->prior->next=info->next;
if(info!=last)
info->next->prior=info->prior;
else
last=info->prior;
}
free(info);
}
struct address*find(name)
char*name;
{
struct addrress*info;
info=start;
while(info){
if(!strcmp(name,info->name))return info;
info=info->next;
}
printf("name not found\n");
return 0;
}
list()
{
register int t;
struct address*info;
info=start;
while(info){
display(info);
info=info->next;
}
printf("\n");
}
display(info)
struct address*info;
{
printf("%s\n",info->name);
printf("%s\n",info->phone);
printf("%s\n",info->build);
printf("\n");
}
void search()
{
char name[20];
struct address*info,*find();
printf("enter name to fing:");
gets(name);
info=find(name);
if(!info)printf("not found\n");
else display(info);
}
save()
{
register int t,size;
struct address*info;
char*p;
FILE*fp;
if((fp=fopen("mlist","wb"))==0){
printf("cannot open file\n");
exit(0);
}
printf("\nsaving file\n");
size=sizeof(list_entry);
info=start;
while(info){
p=(char*)info;
for(t-0;t<size;t++)
putc(*p++,fp);
info=info->next;
}
putc(EOF,fp);
fclose(fp);
}
load()
{ register int t,size;
struct address*info,*temp=0;
char*p;
FILE*fp;
if(fp=fopen("mlist","r"))==0){
printf("cannot open file\n");
size=sizeof(list_entry);
start=malloc(size);
if(!start){
printf("out of memory\n");
return;
}
info=start;
p=(char*)info;
while((*p++=getc(fp))!=EOF){
for(t=0;t<size-1;++t)
*p++=getc(fp);
info->next=malloc(size);
if(!info->next){
printf("out of memory\n");
return;
}
info->prior=temp;
temp=info;
info=info->next;
p=(char*)info;
}
temp->next=0;
last=temp;
start->prior=0;
fclose(fp);
}
struct address*didelete(i,top)
struct address*i;
struct address*top;
{
if(i->prior)i->prior->next=i->next;
else{
top=i->next;
if(top)top->prior=0;
}
if(i->next) i->next->prior=i->prior;
return(top);
}