我这个链表中的init()这个函数错在哪里?
#include <stdio.h>struct student
{
char name[15];
char sex[4];
char QQ[15];
struct student *next;
};
struct student *head=NULL;
void creat()
{
struct student *p1,*p2;
int count=0;
p1=(struct student *)malloc(sizeof(struct student));
memset(p1,0,sizeof(struct student));//格式化内存
scanf("%s%s%s",p1->name,p1->sex,p1->QQ);
p1->next=NULL;
while(strcmp(p1->name,"0")!=0)
{
count++;
if(count==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%s %s %s",p1->name,p1->sex,p1->QQ);
p1->next=NULL;
}
free(p1);
}
void print()
{
struct student *p;
if(head==NULL) printf("lianbiaokong\n");
else
{
p=head;
while(p!=NULL)
{
printf("%s %s %s\n",p->name,p->sex,p->QQ);
p=p->next;
}
}
}
void last()
{
struct student *p2,*p;
p=(struct student *)malloc(sizeof(struct student));
scanf("%s %s %s",p->name,p->sex,p->QQ);
p->next=NULL;
if(head!=NULL)
{
p2=head;
while(p2!=NULL&&p2->next!=NULL)
{
p2=p2->next;
}
p2->next=p;
}
else
head=p;
}
void cover()
{
printf("1创建\n");
printf("2输出\n");
printf("3查询\n");
printf("4修改\n");
printf("5删除结点\n");
printf("6插入结点\n");
printf("7尾部插入结点\n");
printf("8退出程序\n");
}
void search()
{
struct student *p;
char name[15];
if(head==NULL) printf("list kong\n");
else
{
printf("输入要查询的人的姓名:\n");
scanf("%s",name);
p=head;
while(p->next!=NULL&&strcmp(name,p->name)!=0)
p=p->next;
if( strcmp(name,p->name)==0)
{
printf("你要查询的人的信息为:\n");
printf("%s %s %s\n",p->name,p->sex,p->QQ);
}
else
printf("查无此人\n");
}
}
void modify()
{
struct student *p;
char name[15];
if(head==NULL) printf("list kong\n");
else
{
printf("输入要修改的人的姓名:\n");
scanf("%s",name);
p=head;
while(p->next!=NULL&&strcmp(name,p->name)!=0)
p=p->next;
if( strcmp(name,p->name)==0)
{
printf("你要修改的人的信息为:\n");
printf("%s %s %s\n",p->name,p->sex,p->QQ);
printf("重写输入此人的信息:\n");
scanf("%s %s %s",p->name,p->sex,p->QQ);
}
else
printf("查无此人\n");
}
}
void del()
{
struct student *p1,*p2;
char name[15];
if(head==NULL) printf("list kong\n");
else
{
p1=head;
printf("输入要删除的人的姓名:\n");
scanf("%s",name);
while(p1->next!=NULL&&strcmp(name,p1->name)!=0)
{
p2=p1;
p1=p1->next;
}
if( strcmp(name,p1->name)==0)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
else
printf("查无此人\n");
}
free(p1);
}
void insert()
{
struct student *p,*p1,*p2;
char name[15];
p=(struct student *)malloc(sizeof(struct student));
memset(p,0,sizeof(struct student));//格式化内存
scanf("%s%s%s",p->name,p->sex,p->QQ);
p->next=NULL;
if(head==NULL) printf("list kong\n");
else
{
printf("输入要修改的人的姓名:\n");
scanf("%s",name);
p1=head;
while(p1->next!=NULL&&strcmp(name,p1->name)!=0)
{
p2=p1;
p1=p1->next;
}
if( strcmp(name,p1->name)==0)
{
if(p1==head)
{
head=p;
p->next=p1;
}
else
{
p2->next=p;
p->next=p1;
}
}
else
printf("查无此人\n");
}
free(p1);
}
void save()
{
struct student *p;
FILE *fp;
fp=fopen("a.txt","wb");
if(fp!=NULL)
{
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(struct student),1,fp);
p=p->next;
}
fclose(fp);
}
else
printf("文件没有打开");
}
void init()
{
struct student *p1,*p2;
int count=0;
FILE *fp;
fopen("a.txt","rb");
if(fp!=NULL)
{
p1=(struct student *)malloc(sizeof(struct student));
memset(p1,0,sizeof(struct student));//格式化内存
while( fread(p1,sizeof(struct student),1,fp)==1)
{
count++;
if(count==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
memset(p1,0,sizeof(struct student));//格式化内存
}
p1->next=NULL;
free(p1);
fclose(fp);
}
else
printf("文件没有成功打开");
}
main()
{
int choice;
init();
printf("******学生信息管理系统******\n");
while(1)
{
cover();
scanf("%d",&choice);
switch(choice)
{
case 1:creat();save();break;
case 2:print();break;
case 3:search();break;
case 4:modify();save();break;
case 5:del();save();break;
case 6:insert();save();break;
case 7:last();save();break;
case 8:exit(0);break;
default :printf("输入不合法\n");exit(0);
}
}
}