求解答为啥程序会停止运行
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#define recperpage 10
#define FILENAME "c:\\comlists"
struct address
{
char name[20];
char email[20];
char phone[20];
struct address *next;
} *head=NULL;
struct address *addnew (struct address *head,char *name,char *email,char *phone)
{
struct address *p0,*p1;
p0=(struct address *)malloc(sizeof(struct address));
strcpy(p0->name,name);
strcpy(p0->email,email);
strcpy(p0->phone,phone);
if(head==NULL)
head=p0;
else
{
p1=head;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p0;
}
p1->next=NULL;
return(head);
}
struct address *delrec(char *name)
{
struct address *p1,*p2;
if(head==NULL) return NULL;
p1=head;
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("\nDelete OK! \n");
}
else
printf("\nNot find! \n");
return head;
}
void saverec(struct address *head,char *filename)
{
struct address *p=head;
FILE *fp;
if((fp=fopen(filename,"wb"))==NULL)
{
printf("cannot open file\n");
return;
}
if(p!=NULL)
do
{
if(fwrite(p,sizeof(struct address),1,fp)!=1)
{
printf("file write error\n");
fclose(fp);
break;
}
p=p->next;
}while(p!=NULL);
fclose(fp);
}
/*显示链表的数据内容*/
void printrec(struct address *head)
{
int n=0,key;
struct address *p=head;
prints:
printf("\n\nPress 0 to Exit,others to continue!\n");
printf(" ID Name E_Mail Phone\n");
if(p!=NULL)
do
{
printf("%3d",n+1);
printf("%20s",p->name);
printf("%20s",p->email);
printf("%20s",p->phone);
n++;
p=p->next;
if(p==NULL) break;
if(n%recperpage==0)
{
scanf("%d",&key);
if(key==0) break;
else goto prints;
}
}while(p!=NULL);
}
/*从文件中读取数据内容到链表中*/
struct address *readrec(char *filename)
{
char c;
struct address *p,*p1,*head=NULL;
FILE *fp;
if((fp=fopen(filename,"rb"))==NULL)
{
printf("cannot open file\n");
return NULL;
}
while(!feof(fp))
{
c=fgetc(fp); /*预读一个字节,然后再向前定位一个字节*/
if(feof(fp)) break;
fseek(fp,-1,SEEK_CUR);
p=(struct address *)malloc(sizeof(struct address));
fread(p,sizeof(struct address),1,fp);
if(head==NULL)
head=p;
else
p1->next=p;
p1=p;
}
fclose(fp);
p1->next=NULL;
return(head);
} /*交换两个节点的内容*/
void exchangenode(struct address *p,struct address *p1)
{
char cdata[20];
strcpy(cdata,p1->name);
strcpy(p1->name,p1->name);
strcpy(p1->name,cdata);
strcpy(cdata,p1->email);
strcpy(p1->email,p1->email);
strcpy(p1->email,cdata);
strcpy(cdata,p1->phone);
strcpy(p1->phone,p1->phone);
strcpy(p1->phone,cdata);
}
/*按照姓名排序*/
void sortbyname(struct address *head)
{
struct address *p1,*p2,*p;
if(head==NULL) return;
p1=head;
do
{
p=p1;
p2=p1->next;
while(p2!=NULL)
{
if(strcmp(p->name,p2->name)>0)
p=p2;
p2=p2->next;
}
if(p!=p1)
exchangenode(p,p1);
p1=p1->next;
}while(p!=NULL);
}
/*按姓名进行查询并显示查询结果*/
void querybyname(struct address *h,char *qname)
{
struct address *p,*p0=NULL;
p=h;
if(p==NULL)
{
printf("Linklist is NULL,not find!");
return;
}
while(p!=NULL)
{
/*找到查找内容则加入到新的链表中,用于打印*/
if(strcmp(p->name,qname)==0)
p0=addnew(p0,p->name,p->email,p->phone);
p=p->next;
}
/*显示检索出来的链表内容*/
if(p0==NULL)
{
printf("Sorry,Not find!");
return;
}
else
printrec(p0);
}
void mainmenu()
{
printf("\n\n\nWelcome to use this program!\n");
printf("Add New Record,press 1\n");
printf("Delete One Record ,press 2\n");
printf("Modify Record,press 3\n");
printf("Save Record,press 4\n");
printf("Read Record,press 5\n");
printf("Display Record,press 6\n");
printf("Query Record,press 7\n");
printf("Sort Record,press 8\n");
printf("Exit,press 0\n");
}
int main()
{
int key,key1;
char name[20]={0},email[20]={0},phone[20]={0};
mainmenu();
scanf("%d",&key);
while(key!=0)
{
switch(key)
{
case 1: /*添加新纪录*/
printf("Please Input User Name:");scanf("%s",name);
printf("Please Input User E_Mail:");scanf("%s",email);
printf("Please Input User phone:");scanf("%s",phone);
head=addnew(head,name,email,phone); /*调用函数*/
break;
case 2: /*删除一条记录*/
printf("Input User Name to Del:");scanf("%s",name);
head=delrec(name);
break;
case 3: /*修改一条记录*/
printf("input user name to modify:");scanf("%s",name);
head=delrec(name);
break;
case 4: /*保存链表数据到数据文件中*/
saverec(head,FILENAME);
break;
case 5: /*读取数据文件中的记录到链表*/
head=readrec(FILENAME);
break;
case 6: /*显示链表中的数据内容*/
printrec(head);
break;
case 7: /*按照姓名进行查找*/
printf("input user name to Query:");
scanf("%s",name);
querybyname(head,name);
break;
case 8: /*按照姓名排序*/
sortbyname(head);
break;
case 0:
printf("\nThanks,Good_bye!Press any key to Quit\n");
getch();
}
mainmenu();
scanf("%d",&key);
}
return 0;
}