#include
#include
#include
FILE*fp;
structstudent{
charname[10];
charnumber[12];
charage[3];
structstudent*next;
};
structstudent*creat(); //手动创建链表
structstudent*build_list(); //从文件读取信息创建链表
voidread_list(structstudent*head); //打印链表
voidsearch(structstudent*head); //查找相应信息
structstudent*delete_(structstudent*head); //删除相应信息
voidad_list(structstudent*head); //增加信息
intcontrol(structstudent*head); //控制函数
voidhead_print(); //打印界面函数
voidchange_list(structstudent*head); //更改链表信息函数
voidsave(structstudent*head); //保存文件
voidremove(structstudent*head); //递归释放链表空间
charname[10],number[12],age[3]; //学生姓名、学号、年龄全局变量
intmain(){
structstudent*head;
head=(structstudent*)malloc(sizeof(structstudent));
control(head);
free(head);
fclose(fp);
return0;
}
structstudent*creat(){
structstudent*head;
charname[10],number[12],age[3];
structstudent*p,*r,*p1;
head=(structstudent*)malloc(sizeof(structstudent));
p=head->next;
r=head;
printf("依次输入姓名学号年龄(按q退出):\n");
scanf("%s",name);
while(name[0]!='q'){
p1=(structstudent*)malloc(sizeof(structstudent));
scanf("%s",number);
scanf("%s",age);
strcpy(p1->name,name);
strcpy(p1->number,number);
strcpy(p1->age,age);
p1->next=NULL;
r->next=p1;
r=p1;
scanf("%s",name);
}
printf("建立成功\n");
returnhead;
}
structstudent*build_list(){
charch;
structstudent*head;
structstudent*p1,*r;
rewind(fp);
head=(structstudent*)malloc(sizeof(structstudent));
r=head;
while(!feof(fp)){
p1=(structstudent*)malloc(sizeof(structstudent));
fscanf(fp,"%s%s%s",p1->name,p1->number,p1->age);
p1->next=NULL;
r->next=p1;
r=p1;
fseek(fp,2L,SEEK_CUR);
if((ch=getc(fp))==EOF)
break;
else
fseek(fp,-3L,SEEK_CUR);
}
returnhead;
}
voidread_list(structstudent*head){
structstudent*p;
p=head->next;
if(p==NULL){
head=creat();
}
printf("*******************************************\n");
while(p!=NULL){
printf("%-20s%-12s%-3s\n",p->name,p->number,p->age);
p=p->next;
}
printf("*******************************************\n");
}
voidsearch(structstudent*head){
chars[20];
structstudent*p;
intk=0;
printf("请输入要查询的字符串:\n");
p=head->next;
scanf("%s",s);
printf("查询结果:\n");
while(p!=NULL){
if((strcmp(s,p->age)&&strcmp(s,p->name)&&strcmp(s,p->number))==0){
printf("%-20s%-12s%-3s\n",p->name,p->number,p->age);
k=1;
}
p=p->next;
}
if(k==0)
printf("无记录\n");
}
structstudent*delete_(structstudent*head){
chars[20];
charch;
intv=0;
structstudent*p,*q;
printf("请输入要删除的学生的学号:\n");
scanf("%s",s);
p=head->next;
q=head;
while(p!=NULL){
if(strcmp(s,p->number)==0){
q->next=p->next;
v=1;
free(p);
break;
}
q=p;
p=p->next;
}
if(v==1)
printf("删除成功\n");
else{
printf("未删除成功\n");
returnq->next;
}
if(head->next==NULL){
printf("已无信息,是否输入新的(Y/N):\n");
scanf("%c",&ch);
ch=getchar();
if(ch=='y')
head=creat();
returnhead;
}
returnq->next;
}
voidad_list(structstudent*head){
chars[20];
intv=0;
structstudent*p,*q;
q=(structstudent*)malloc(sizeof(structstudent));
p=head->next;
printf("插在学号为多少的学生后?\n");
scanf("%s",s);
while(p!=NULL){
if(strcmp(s,p->number)==0){
printf("请输入学生信息(姓名学号年龄):\n");
scanf("%s%s%s",q->name,q->number,q->age);
q->next=p->next;
p->next=q;
v=1;
break;
}
p=p->next;
}
if(v==0)
printf("插入失败\n");
}
voidchange_list(structstudent*head){
chars1[12];
charch;
structstudent*p;
printf("请输入要更改的学生的学号:\n");
scanf("%s",s1);
p=head->next;
while(p!=NULL){
if(strcmp(s1,p->number)==0)
break;
p=p->next;
}
printf("请输入要更改的内容:\na.姓名\nb.学号\nc.年龄\n");
scanf("%c",&ch);
ch=getchar();
printf("请输入更改后结果:\n");
scanf("%s",s1);
switch(ch){
case'a':strcpy(p->name,s1);break;
case'b':strcpy(p->number,s1);break;
case'c':strcpy(p->age,s1);break;
}
}
voidsave(structstudent*head){
structstudent*p;
p=head->next;
fp=fopen("stu.out","w+");
rewind(fp);
while(p!=NULL){
fprintf(fp,"%-20s%-12s%3s\n",p->name,p->number,p->age);
p=p->next;
}
printf("保存成功\n");
}
voidremove(structstudent*head){
if(head==NULL)
return;
remove(head->next);
free(head);
}
intcontrol(structstudent*head){
inti=0;
charch;
head_print();
if((fp=fopen("stu.out","r"))==NULL){
printf("无文件,请先建立:\n");
head=creat();
}
elseif((ch=getc(fp))==EOF){
printf("无信息,请先建立:\n");
head=creat();
}
else
head=build_list();
while((ch=getchar())!='q'){
switch(ch){
case'a':search(head);break;
case'b':delete_(head);break;
case'c':ad_list(head);break;
case'd':change_list(head);break;
case'e':read_list(head);break;
case'f':save(head);break;
case'h':head_print();
}
}
if(ch=='q')
remove(head);
return0;
}
voidhead_print(){
printf("%13.0s********************************************%13.0s\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0sa.查找%9.0sb.删除%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0sc.增加%9.0sd.更改%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0se.查看%9.0sf.存档%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0s按h显示提示信息,q退出%10.0s**\n");
printf("%13.0s********************************************%13.0s\n");
}