#include
#include
#include
#include
#include
struct people
{
char name[20];
char phone[30];
char email[30];
struct people *next;
};
int main()
{
struct people * head;
struct people *creat();
void sort(struct people *head);
head=creat();
sort(head);
return 0;
}
struct people creat()
{
struct people * head;
struct people *p1,*p2;
void save(struct people *p1);
int i,n;
p1=p2=(struct people)malloc(sizeof(struct people));
i=1;
n=0;
head=NULL;
printf("请输入:\n");
while(i==1)
{
printf("姓名:");
scanf("%s",&p1->name);
printf("电话:");
scanf("%s",&p1->phone);
printf("Email:");
scanf("%s",&p1->email);
save(p1);
if(n==0) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people *)malloc(sizeof(struct people));
n=n+1;
printf("继续输入请按1,退出请按0.\n");
scanf("%d",&i);
}
p2->next=NULL;
return(head);
}
void save(struct people *p)
{
FILE *fp;
char name[20];
printf("文件名称:");
scanf("%s",name);
if((fp=fopen(name,"wb"))==NULL)
{
printf("cannot open file\n");
return;
}
if(fwrite(p,sizeof(struct people),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void sort(struct people *head)
{
struct people *p1,*p2,*tail,*p;
tail=NULL;
while((head->next->next)!=NULL)
{
p1=head;
p2=head->next;
while((p1->next->next)!=NULL)
{
if(strcmp((p1->name),(p1->next->name))>0)
{
p1->next=p2->next;
p2->next=p2->next->next;
p1->next->next=p2;
p2=p1->next;
}
p1=p1->next;
p2=p2->next;
}
tail=p2;
}
p=head;
do
{printf("姓名:%s 电话:%s\n",p->name,p->phone);
p=p->next;
}while(p!=NULL);
}