相较于数组,链表对于数据的增加和删除更加方便,但是对于查找确增加了难度。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include <string.h>
#define len sizeof(struct student)
//定义结构体
struct student{int num;//学号 char name[15];//姓名 char major[10];//专业computer,softwear,networkint classNo;//班级 int score[3];//3门课的成绩 int sum;//3门课的总成绩 struct student *next;//指针 88u};
//定义全局变量
int n;int a;
//函数声明
struct student *input();
struct student *creatlist(char *filename);
void save(student *head);
void print(struct student*head);void print1(struct student*head);
void del(struct student *head,int x);
struct student * insert(struct student*head);
void outone(struct student *temp);
struct student *MAX(struct student*head);
struct student *numfind(struct student *head);
struct student *search_major_score(struct student *head);
struct student *del_class_score(struct student *head);
//主函数
int main()
{char s[15];struct student *head;while(1){printf("请输入功能编号完成相应功能\n");//printf("1.输入学生信息并保存\n");printf("1.从文件中读取学生信息,并建立链表\n"); printf("2.显示链表中各个学生信息\n");printf("3.查找总成绩最高的学生\n");printf("4.寻找指定学号的学生 \n");printf("5.按学号插入学生信息\n"); printf("6.按学号删除学生信息\n");printf("7.查找某个专业、某门课程的成绩小于某个分数的学生\n");printf("8.删除某个班级、某门课程的成绩小于某个分数的学生\n");printf("9.保存当前信息\n");printf("其他,推出系统\n");int x,y;struct student *h,*k;scanf("%d",&x);switch(x){/*case(1)://输入学生信息并保存h=input();save(h);break;*/case(1)://从文件中读取学生信息,并建立链表printf("请输入要查找的文件名:\n");getchar();gets(s);head=creatlist(s);printf("\n");printf("注册链表成功\n"); break;case(2)://显示链表中学生信息 print(head);break;case(3)://查找总成绩最高的学生printf("总成绩最高的学生信息如下\n");printf("\n学号\t姓名\t专业 \t\t班级 成绩0 成绩1 成绩2 \t\n"); outone(MAX(head));break;case(4)://寻找指定学号的学生printf("请输入要查找的学生的学号:\n");numfind(head);break;case(5)://按学号插入学生信息printf("请按顺序输入要插入的信息,用空格隔开\n");insert(head);//print(head);break;case(6)://按学号删除学生信息printf("请输入要删除的学生的学号:\n");scanf("%d",&y);del(head,y);break;case(7)://查找某个专业、某门课程的成绩小于某个分数的学生print(head=search_major_score(head));break;case(8)://删除某个班级、某门课程的成绩小于某个分数的学生head=del_class_score(head);print(head);break;case(9)://保存当前信息save(head);break;default:break;}}return 0;
}
//输入学生信息
struct student *input()
{n=0;printf("请输入学生人数\n");scanf("%d",&a);printf("请录入这%d名学生的信息:\n",a);struct student *head;struct student *p1,*p2;head=(struct student*)malloc(len);while(n<a){p1=(struct student*)malloc(len);scanf("%d%s%s%d%d%d%d",&p1->num,p1->name,p1->major,&p1->classNo,&p1->score[0],&p1->score[1],&p1->score[2]);p1->sum=0;p1->sum=p1->score[0]+p1->score[1]+p1->score[2];n++;if(n==1)head->next=p1;elsep2->next=p1;p2=p1;}p2->next=NULL;return(head);
}
//寻找成绩最高的学生
struct student *MAX(struct student*head)
{struct student *p,*maxn,*max,*q;p=head;int big;big=p->score[0]+p->score[1]+p->score[2];maxn=p;while(p!=NULL){//printf("%d\n",big);if((p->score[0]+p->score[1]+p->score[2])>big){//outone(p);big=p->score[0]+p->score[1]+p->score[2];maxn=p;//outone(maxn);}p=p->next;}max->next=maxn;while(p!=NULL&&p->next!=NULL) //寻找分数最大节点 {p=p->next;if(p->sum>=big) //比较 {big=p->sum;maxn=p;}}return(max);
}
//从文件中读取学生信息,并建立链表
struct student *creatlist(char *filename)
{student *s,*p,*head; //s为当前节点 ,head为头节点FILE *fp;if((fp=fopen(filename,"rb"))==NULL){printf("Wrong.");exit(0);}head=(student *)malloc(sizeof(student)); //开辟内存 s=(student *)malloc(sizeof(student)); //开辟内存if((fread(s,sizeof(student),1,fp))==NULL){printf("Wrong.");exit(0); } head->next=s;p=s;while(s->next!=NULL){s=(student *)malloc(sizeof(student));fread(s,sizeof(student),1,fp);p->next=s;p=s;}return head;
}
//保存数据文件
void save(student *head)
{struct student *p;p=head->next;//从第二个开始保存的 FILE *fp;char filename[15];printf("请输入要建立的文件的名称:\n"); scanf("%s",&filename);if((fp=fopen(filename,"wb"))==NULL){printf("Wrong.");exit(0);} while(p!=NULL){if(fwrite(p,sizeof(student),1,fp)!=1){printf("写入文件失败\n"); }p=p->next;}printf("写入文件成功!\n"); fclose(fp);
}
//输出一个节点信息
void outone(struct student *temp)
{temp=temp->next;printf("%d\t%-6s\t%-10s\t%d\t%d\t%d\t%d\t\n", temp->num, temp->name, temp->major,temp->classNo,temp->score[0],temp->score[1],temp->score[2]);
}
//打印无头节点
void print(struct student*head)
{struct student *p;printf("\n学号\t姓名\t专业 \t\t班级 成绩0 成绩1 成绩2 \t"); p=head->next;while(p!=NULL){printf("\n%d\t%-6s\t%-10s\t%d\t%d\t%d\t%d\t", p->num, p->name, p->major,p->classNo,p->score[0],p->score[1],p->score[2]);p=p->next;}printf("\n");
}
//打印有头节点
void print1(struct student*head)
{struct student *p;p=head;printf("\n学号\t姓名\t专业 \t\t班级 成绩0 成绩1 成绩2 \t"); while(p!=NULL){printf("\n%d\t%-6s\t%-10s\t%d\t%d\t%d\t%d\t", p->num, p->name, p->major,p->classNo,p->score[0],p->score[1],p->score[2]);p=p->next;}printf("\n");
}
//按学号删除学生信息
void del(struct student *head,int x)
{struct student *p,*q,*k;q=head;p=head->next;if(head==NULL)printf("Wrong.\n");while(p->num!=x&&p->next!=NULL){q=q->next;p=p->next;}if(p->num==x){//printf("被删除的学生的信息是:\n");//printf("\n学号\t姓名\t专业 \t\t班级 成绩0 成绩1 成绩2 \t\n"); //outone(p);printf("\n");printf("确定要删除此人信息?输入'1'确认删除,输入'0'取消删除:\n");int y;scanf("%d",&y);if(y==1){q->next=p->next;printf("删除成功!\n");printf("新的学生信息如下:\n");print(head);}if(y==0){printf("取消删除成功!\n");}}else{printf("很抱歉,系统查无此人\n"); }
}
//按学号插入学生信息
struct student *insert(struct student*head)
{struct student *temp;struct student *p;temp=(student *)malloc(sizeof(student));p=head;printf("\n学号\t姓名\t专业 \t\t班级 成绩0 成绩1 成绩2 \t\n"); scanf("%d%s%s%d%d%d%d",&temp->num,&temp->name,&temp->major,&temp->classNo,&temp->score[0],&temp->score[1],&temp->score[2]);temp->sum=temp->score[0]+temp->score[1]+temp->score[2];while(p!=NULL){if(p->next->num>temp->num){temp->next=p->next;p->next=temp;break;}else{p=p->next;if(p->next==NULL){p->next=temp;temp->next=NULL;break;}}}return(head);
}//寻找指定学号的学生
struct student *numfind(struct student *head)
{int x;scanf("%d",&x);struct student *p;p=head;while(p!=NULL){if(p->num==x){printf("\n学号\t姓名\t专业 \t\t班级 成绩0 成绩1 成绩2 \t\n"); printf("%d\t%s\t%s \t%d\t%d\t%d\t%d\n",p->num,p->name,p->major,p->classNo,p->score[0],p->score[1],p->score[2]);break;}p=p->next;}return(head);
}
//查找某个专业、某门课程的成绩小于某个分数的学生
struct student *search_major_score(struct student *head)
{struct student *p,*q,*xin,stu1;q=(student *)malloc(sizeof(student));xin=q;p=head;char a[15];int x,i;printf("请输入专业:"); scanf("%s",a);printf("请输入课程编号(0-2):");scanf("%d",&i);printf("请输入分数:");scanf("%d",&x);while(p!=NULL){if(strcmp(p->major,a)==0){if(p->score[i]<x){q->next=(student *)malloc(sizeof(student)); q=q->next; q->num=p->num;strcpy(q->major,p->major);strcpy(q->name,p->name);q->score[0]=p->score[0];q->score[1]=p->score[1];q->score[2]=p->score[2];q->sum=p->sum;q->classNo=p->classNo;}}p=p->next;}q->next=NULL;return(xin);
}
//删除某个班级、某门课程的成绩小于某个分数的学生
struct student *del_class_score(struct student *head)
{struct student *p,*q;q=head;p=head->next;int a,i,x;printf("请输入班级:"); scanf("%d",&a);printf("请输入课程编号(0-2):");scanf("%d",&i);printf("请输入分数:");scanf("%d",&x);while(p!=NULL){if(p->classNo==a&&p->score[i]<x){q->next=p->next;p=q->next;}else{p=p->next;q=q->next; }}q=NULL;return(head);
}