C语言使用单链表实现学生信息管理系统
程序员文章站
2022-06-27 19:01:39
本文实例为大家分享了c语言使用单链表实现学生信息管理系统,供大家参考,具体内容如下初学数据结构,记录一下学习过程。运行结果如图:1.运行界面2.录入学生信息3.按照总分进行排序代码如下:#define...
本文实例为大家分享了c语言使用单链表实现学生信息管理系统,供大家参考,具体内容如下
初学数据结构,记录一下学习过程。
运行结果如图:
1.运行界面
2.录入学生信息
3.按照总分进行排序
代码如下:
#define error 0 #define ok 1 #define overflow -1; typedef int elemtype; typedef int status; #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> #include<iostream> typedef struct{ char name[20]; char snumber[20]; char sex[10]; int math; int chinese; int english; }student; typedef struct lnode{ student data; struct lnode *next; }lnode,*linklist; //单链表初始化 status initlist(linklist &l){ l = new lnode; l->next = null; return ok; } //创建单链表 void createlist(linklist &l,int n){ linklist r; linklist p; l = new lnode; l->next = null; r = l; int i; for(i = 0;i<n;i++){ p = new lnode; scanf("%s%s%s%d%d%d",&p->data.name,&p->data.snumber,&p->data.sex,&p->data.chinese,&p->data.math,&p->data.english); //scanf("%d%d%d",&p->data.chinese,&p->data.math,&p->data.english); p->next=null; r->next=p; r = p; } } //总分统计 int gradesum(linklist l){ int sum; sum = (l->data.chinese)+(l->data.english)+(l->data.math); return sum; } //统计所有学生信息 void bianli(linklist l){ linklist p; p = l->next; printf("姓名\t学号\t性别\t语文\t数学\t英语\t总分\n"); while(p){ printf("%s\t%s\t%s\t",p->data.name,p->data.snumber,p->data.sex); printf("%d\t%d\t%d\t%d",p->data.chinese,p->data.math,p->data.english,gradesum(p)); p = p->next; printf("\n"); } } //按照总分排序 void ordersum(linklist &l){ linklist p,q,tail; tail = null; while((l->next->next) != tail) { p = l; q = l->next; while(q->next != tail) { if( gradesum(q) < gradesum(q->next)) { p->next = q->next; q->next = q->next->next; p->next->next = q; q = p->next; } q = q->next; p = p->next; } tail = q; } printf("排序完毕!\n"); } //根据语文进行排序 void orderchinese(linklist &l){ linklist p,q,tail; tail = null; while((l->next->next) != tail) { p = l; q = l->next; while(q->next != tail) { if((q->data.chinese) < (q->next->data.chinese)) { p->next = q->next; q->next = q->next->next; p->next->next = q; q = p->next; } q = q->next; p = p->next; } tail = q; } printf("排序完毕!\n"); } //根据英语进行排序 void orderenglish(linklist &l){ linklist p,q,tail; tail = null; while((l->next->next) != tail) { p = l; q = l->next; while(q->next != tail) { if((q->data.english) < (q->next->data.english)) { p->next = q->next; q->next = q->next->next; p->next->next = q; q = p->next; } q = q->next; p = p->next; } tail = q; } printf("排序完毕!\n"); } //根据数学进行排序 void ordermath(linklist &l){ linklist p,q,tail; tail = null; while((l->next->next) != tail) { p = l; q = l->next; while(q->next != tail) { if((q->data.math) < (q->next->data.math)) { p->next = q->next; q->next = q->next->next; p->next->next = q; q = p->next; } q = q->next; p = p->next; } tail = q; } printf("排序完毕!\n"); } //删除学生信息 status listdelete(linklist &l,int i){ linklist p; linklist q; int j; p = l;j = 0; while((p->next)&&(j<i-1)){ p = p->next; ++j; } if(!(p->next)||(j>i-1)) return error; q = p->next; p->next = q->next; delete q; return ok; } void deletestudent(linklist &l){ int i; printf("请输入需要删除的学生序号\n"); scanf("%d",&i); listdelete(l,i); printf("删除完毕!\n"); } //插入学生信息 status listinsert(linklist &l,int i,char name[],char snumber[],char sex[],int chinese,int math,int english){ linklist p = l; linklist s; int j = 0; while(p && (j<i-1)){ p = p->next; ++j; } if(!p||j>i-1) return error; s = new lnode; strcpy(s->data.name,name); strcpy(s->data.snumber,snumber); strcpy(s->data.sex,sex); s->data.chinese = chinese; s->data.math = math; s->data.english = english; s->next = p->next; p->next = s; return ok; } void insertstudent(linklist &l){ int n; char name[20],snumber[20],sex[10]; int chinese,math,english; printf("请输入插入的位置n:"); scanf("%d",&n); printf("请依次输入学生的姓名、学号、性别、语文成绩、数学成绩、英语成绩、总分\n"); scanf("%s%s%s%d%d%d",&name,&snumber,&sex,&chinese,&math,&english); listinsert(l,n,name,snumber,sex,chinese,math,english); } void menu(){ printf("\t\t\t ======================================================\n\n"); printf("\t\t\t ‖ 学生信息管理系统 ‖ \n\n"); printf("\t\t\t ======================================================\n\n"); printf("\t\t\t ===================系统功能菜单=======================\n"); printf("\t\t\t ‖ ------------------------------------------------ ‖\n"); printf("\t\t\t ‖ ------------------------------------------------ ‖ \n"); printf("\t\t\t ====================================================== \n"); printf("\t\t\t ‖ 0.录入学生信息 ‖ ‖ 1.统计学生信息 ‖ \n"); printf("\t\t\t ======================================================\n"); printf("\t\t\t ‖ 2.按照总分排序 ‖ ‖ 3.按照语文成绩排序 ‖ \n"); printf("\t\t\t ====================================================== \n"); printf("\t\t\t ‖ 4.按照数学成绩排序 ‖ ‖ 5.按英语成绩排序 ‖ \n"); printf("\t\t\t ======================================================\n"); printf("\t\t\t ‖ 6.删除学生信息 ‖ ‖ 7.插入学生信息 ‖ \n"); printf("\t\t\t ====================================================== \n"); printf("\t\t\t ‖ 8.退出系统 ‖ \n"); printf("\t\t\t ========================== \n"); printf("\t\t\t ----------------------------------------------- \n"); printf("\n\n\n"); } void luru(linklist &l){ int n; printf("请输入学生数目n:"); scanf("%d",&n); printf("请依次输入学生的姓名、学号、性别、语文成绩、数学成绩、英语成绩、总分\n"); createlist(l,n); printf("学生信息已录入完毕!\n"); } void main(int args,char argv[]){ linklist p; initlist(p); int i; do{ menu(); scanf("%d",&i); getchar(); switch(i){ case 0:luru(p);break; case 1:bianli(p);break; case 2:ordersum(p);bianli(p);break; case 3:orderchinese(p);bianli(p);break; case 4:ordermath(p);bianli(p);break; case 5:orderenglish(p);bianli(p);break; case 6: deletestudent(p);break; case 7:insertstudent(p);break; case 8:exit(0); } }while(i != -1); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。