表格模板-利用单项链表实现简单的学生信息管理 精品
- 格式:doc
- 大小:128.50 KB
- 文档页数:8
C课程设计单链表学生信息管理系统学生信息管理系统设计文档一、设计任务描述为了实现学籍管理的简单化,我们基于Visual C++集成开发环境编写了”学生信息管理系统”软件, 该软件适用于所有windows 操作系统, 面向广大用户, 界面简洁, 操作简单。
此软件主要是实现对学生学籍信息进行系统化的管理, 能够对学生基本信息进行添加、删除、查找、修改以及对学生成绩的管理, 主要是根据学生的学号及其姓名进行操作的。
该软件能够更加方便管理者管理学生学籍信息。
功能需求说明该系统所需要的功能有: 1、链表的建立2、学生信息的插入;3学生信息的查询;、4学生信息的输出;、5学生信息的修改;、6学生信息的删除;、7良好的欢迎选择界面。
、三、总体方案设计一、实现任务的方法1、在欢迎选择界面中, 使用Switch 这一选择结构来连接程序的执行和用户的命令;2、在从学生信息的建立直到删除, 都是使用链表的相关知识3、在定义学生信息时,建立一个Inform类;在定义学生课程成绩时,自定义了一个achieve结构体;总体结构ZJ建立链表三、模块划分(1) 链表的建立。
(2) 对链表信息的插入。
(3) 对链表信息的查找。
(4) 对链表信息的输出。
(5) 对链表信息的删除。
(6) 对链表信息的修改。
课程成绩信息作为附加信息,穿插于各个模块中三、数据结构说明」、自定义的数据结构资料内容仅供参考,如有不当或者侵权,请联系本人改正或者删除。
1、achieve (课程成绩)用于存放课程成绩信息包括课程数、课程名、成绩、学分、总分和平均分"谍稈数〃课程名(最參课程数为nn "学分 "总分"平均分 "默认枸匯雷数"计算该学生课程的加权平均分(总咸绩/总学分)2、inform (学生基本信息)用于存放学生基本信息,包括姓名、 学号、性别等。
"元素类型 "姓容 〃学号 “性别 "身份证号"出生年月曰 "家庭地址 "电话号码 "课程咸绩〃谍程咸绩输入 "遥程成缢输出3、结点结构-Nodetype,定义了数据域inform 和指针域next; structNodetype//结点结构{inform data; /7数据域Node type 切氏t; 〃指针域};Student 用于存放处理学生信息的各个功能函数,private 成员是链表的头指针。
利用单项链表实现简单的学生信息管理一、题目:利用单项链表实现简单的学生信息治理(07)二、设计思路1、总体设计1)分析程序的功能创建单项链表储存学生的各项信息,学号、姓名、成绩。
并能够完成学生信息的插入、删除及信息的显示功能。
2)系统总体结构:按照程序要求的功能采纳结构化的设计思想,划分为五个功能模块,即创建链表、插入函数、删除函数、显示函数和主函数。
2、各功能模块的设计:讲明各功能模块的实现方法①头文件:对自己定义的函数进行函数声明。
②主函数:进行函数的调用,实现各函数的功能,达到预期的目的。
③函数定义部分:定义各个功能函数,创建链表函数、插入新信息函数、删除信息函数、显示信息函数。
3、设计中的要紧困难及解决方案1)在插入新信息时,有插入点在表头、中间、表尾三种情形,为此采纳讨论的方法,把三种情形进行讨论使其分开进行。
2)在删除信息时,有删除的为头结点和中间结点的情形,采纳讨论的方法,把两种情形分开来进行。
4、你所设计的程序最终完成的功能1)创建链表、插入新信息、删除信息、显示信息。
2)测试数据①输入的数据99812 LiuLifang 91学号姓名成绩96085 WangLiPing 7798120 ZhangLi 7599912 LiuHai 80 ③删除的数据学号姓名成绩99812 liulifang 91运行结果三、程序清单本程序包含creatlist.cpp、insert.cpp、del.cpp、output.cpp、main.cpp、头文件.h六个文件#include<iostream.h>#include"头文件.h"int n;student *creatlist(){student *head;student *p1;student *p2;n=0;head=NULL;p1=new(student); //创建一个新结点p2=p1;cin>>p1->num>>p1->name>>p1->score;while(p1->num!=0) //链表建立过程终止的判定条件{n++;head=p1;elsep2->next=p1; //原链表结点指向新建结点p2=p1;p1=new(student);cin>>p1->num>>p1->name>>p1->score;}delete(p1);p2->next=NULL;return head; //返回表头}2、insert.cpp文件清单#include<iostream.h>#include"头文件.h"student *insert(student *head,student *t){student *p0; //待插入点student *p1;student *p2; //p0插入p1之前,p2之后p1=head;p0=t;if(p1==NULL) //原链表是空表{head=p0;p0->next=NULL;}elsewhile((p0->num>p1->num)&&(p1->next!=NULL)) //查找待插入点{p2=p1;p1=p1->next;}if(p0->num<=p1->num){if(p1==head) //要插入的位置在表头{head=p0;p0->next=p1;}else //要插入的位置不是表头{p2->next=p0;p0->next=p1;}}else //插入表尾结点之后{p1->next=p0;p0->next=NULL;}}return head; //返回表头}3、del.cpp文件清单#include<iostream.h>#include"头文件.h"student *del(student *head,int num){student *p1;student *p2;if(head==NULL) //原链表是空表{cout<<"List is NULL\n";return head;}p1=head;while((num!=p1->num)&&(p1->next!=NULL)) //查找带要删除的结点{p2=p1;p1=p1->next;if(num==p1->num){if(p1==head) //要删除的是头结点head=p1->next;else //要删除的不是头结点p2->next=p1->next;delete(p1); //开释被删除结点所占的内存空间cout<<"delete: "<<num<<endl;}elsecout<<"not found"<<endl;return head; //返回表头}4、output.cpp文件清单#include<iostream.h>#include"头文件.h"void output(student *head){if(head==NULL) //原链表是空表cout<<"list is NULL\n";else{student *p1;p1=head;cout<<"学生的成绩信息"<<endl;cout<<"学号"<<"\t姓名"<<"\t成绩\n";do //输出链表中各个同学的信息{cout<<p1->num<<"\t"<<p1->name<<"\t"<<p1->score<<endl;p1=p1->next;}while(p1!=NULL);}}5、main.cpp文件清单#include<iostream.h>#include"头文件.h"void main(){student *headl;cout<<"输入学生的成绩信息"<<endl;cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;headl=creatlist();int k;{cout<<endl;cout<<"--------菜单选项---------"<<endl;cout<<"1.插入新信息 ,请选择:1"<<endl;cout<<"2.删除信息, 请选择:2"<<endl;cout<<"3.显示信息, 请选择:3"<<endl;cout<<"4.终止程序, 请选择:4"<<endl;cout<<" 选择";cin>>k;if(k==1) //插入新信息{int m;cout<<"输入插入学生人数"<<endl;cin>>m;cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;for(int i=0;i<m;i++){student *stu;stu=new(student);cin>>stu->num>>stu->name>>stu->score;headl=insert(headl,stu);}}else if(k==2) //删除信息{int num;cout<<"输入要删除学生的学号\n";cin>>num;headl=del(headl,num);}else if(k==3) //显示信息{output(headl);}else //终止程序break;};}6、头文件.h文件清单struct student //定义结构体类型{int num;char name[20];student *next;};student *creatlist(); //创建链表函数原型讲明student *insert(student * ,student * ); //插入函数原型讲明student *del(student * ,int ); //删除函数原型讲明void output(student * ); //显示函数原型讲明四、对该设计题目有何更完善的方案1、对自己完成程序进行自我评判。
数据结构单链表应用(简易学生信息管理系统)1这是主文件建立文件“student-main.cpp”粘贴保存#include#include#include#include#include"student-fun.cpp"#define PRINT "%6d %12s %5.2f\n",p->num,p->name,p->score //定义输出格式#define PRINTF " 学号姓名成绩\n"char cmd;int num;lklist L;void jiemian(){ system("cls");printf("\n***************************************************** ****************** *");printf("\n****** 欢迎使用简易班级学生信息系统********");printf("\n**************");printf("\n******i--增加学生d--删除学生q--退出系统********");printf("\n****** f--查询c--修改l--列出所有学生信息********");printf("\n***************************************************** ****************** *\n");}void ReadCommand(){do {printf("\n 请根据提示输入命令: ");fflush(stdin); //清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件scanf("%c",&cmd);} while (cmd!='i'&&cmd!='d'&&cmd!='l'&&cmd!='q'&&cmd!='f'&&c md!='c');}void Interpret()switch(cmd){case 'i': insert_lklist(L); break;case 'd': del_lklist(L);break;case 'l': list_lklist(L); break;case 'f': find_lklist(L);break;case 'c': cor_lklist(L);break;case 'q': exit(0);}}void main(){L=initial_lklist();do { jiemian();ReadCommand();Interpret();system("pause"); //暂停,按任意键继续。
实验2利用单链表实现学生信息管理1.实验目的·掌握单链表结构的实现方式;掌握单链表常用算法的实现;·熟悉利用单链表解决问题的一般思路;了解单链表结构的优点与不足。
2.实验内容与要求以学生信息为数据结点建立带头结点的单链表。
一个结点的信息包括学号、姓名、性别、班级和联系电话。
程序用户可通过数字键选择信息浏览、插入信息、修改信息、删除信息等功能。
对程序的具体要求如下:(1)程序启动后,显示下列选项信息:1—信息浏览2—插入信息3—删除信息4-修改信息0—退出程序(2)输入数字“1”显示所有学生信息列表。
(3)输入数字“2”进入插入信息功能模块。
程序依次提示并由用户输入学号、姓名、性别、班级和联系电话,最终实现在线性表头部插入一个学生信息。
(4)输入数字“3”进入删除信息功能模块。
程序提示并由用户输入学号,最终实现按照学号删除某个学生信息。
(5)输入数字“4”进入修改信息功能模块。
程序提示并由用户输入学号,最终实现按照学号修改某个学生信息。
(6)通过输入数字“0”使得程序结束。
(7)当用户执行浏览、插入、删除功能后,程序应继续提示用户通过数字键选择相应功能,直到用户输入数字“0”程序才结束。
3.实验编程指导(1)可仿照本章单链表的定义形式,建立学生信息结点数据,并建立一个空的单链表。
(2)参考本章中单链表的算法描述和例题中的算法实现,在本程序中增加信息浏览、插入结点、删除结点、修改结点内容的算法实现函数。
(3)编写主函数,可通过在while循环结构中嵌入switch分支结构实现操作选择功能。
(4)完善插入、删除功能。
实现插入信息的录入、删除记录的定位等细节。
并完善用户操作界面,给出详细的操作提示。
5.实验报告要求实验报告要求以word文件形式发到老师邮箱。
内容包括:(1)报告封面包括实验名称、班级、姓名、学号以及实验完成日期。
(2)各程序模块名称及功能说明。
并绘制出主要功能函数的程序流程图。
#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 15 //学号位数#define M 15 //姓名字节数#define L 4 //成绩的科数int aa=0;//记录筛选时打开的文void printf_(struct stu *); //输出单个的学生数据struct stu * scanf1_(); //接收单个的学生数据,返回指针,struct stu * scanf_(); //得到学生的数据,并放入到链表中void print_(struct stu *); //输出链表中的学生信息struct stu *num_paixu(struct stu *); //用链表给学生排序void chaxun(struct stu *); //根据学生的学号查询学生的信息,并把学生的数据输出struct stu * charu(struct stu *); //将学生的数据插入到顺序排放的链表中,并且插入后也是顺序排放的。
struct stu * shanchu(struct stu *); //删除数据void xiugai(struct stu *); //修改学生的信息。
void fprint_(struct stu *); //把数据写入到文件int to_ji(struct stu *); //统计学生的总人数int t_ji_tj(struct stu *); //统计成绩在某一区间内的人数struct stu * ch_ji_paixu(struct stu *); //按成绩排序,默认的是按总成绩排序struct s *shaixuan(struct stu *); //筛选符合条件的数据,得到符合条件的结构体数据的指针。
void shuchu_shai(struct s *); //输出筛选出来的数据void fb_shu_shai(struct s *); //将筛选出来的数据输入到文件中void avrage(struct stu *head,float *); //计算平均成绩void fb_avrage(float *); //将平均成绩放在文件的最后面//建立学生结构体struct stu{char num[N];char name[M];int ch_ji[L];struct stu *next;};//建立一个链表,存放学生结构体的指针struct s{struct stu *p;struct s *ps;};void main(){int i,n,x=1,k=0;float a[L]={0};struct stu *head=NULL;struct s *shead=NULL;char ch;while(x){ system("cls");printf("********************************************************************* **********\n");printf(" 0---退出系统1---录入数据\n");printf(" 2---保存文件3---按成绩排序\n");printf(" 4---查看信息5---按学号排序\n");printf(" 6---统计人数7---按条件筛选\n");printf(" 8---查询信息9---显示筛选结果\n");printf(" 10---插入数据11---保存筛选结果\n");printf(" 12---删除数据13---计算平均成绩\n");printf(" 14---修改数据15---保存平均成绩\n");printf(" 16---按条件统计人数\n");printf("********************************************************************* **********\n");scanf("%d",&n);getchar();//有啥作用switch(n){case 0:x=0;break;case 1:head=scanf_();break;case 2:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}elsefprint_(head);break;case 3:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{head=ch_ji_paixu(head);break;}case 4:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}elseprint_(head);break;case 5:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{head=num_paixu(head);printf("是否显示排序结果Y/N ?");ch=getchar();getchar();if(ch=='y'||ch=='Y'){printf("\n");print_(head);}}case 6:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{k=to_ji(head);printf("总共有%3d 名学生",k);getchar();break;}case 7:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{shead=shaixuan(head);if(shead!=NULL&&shead->ps!=NULL){printf("是否显示筛选结果Y/N ?");ch=getchar();if(ch=='y'||ch=='Y'){printf("\n");shuchu_shai(shead);getchar();}}break;}case 8:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();}else{chaxun(head);getchar();break;}case 9:if(head==NULL||head->next==NULL) {printf("请先录入学生的数据!");getchar();break;}else{printf("\n");shuchu_shai(shead);getchar();break;}case 10:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}elsehead=charu(head);break;case 11:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{fb_shu_shai(shead);getchar();break;}if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{head=shanchu(head);break;}case 13:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{avrage(head,a);printf("平均值计算成功!\n是否显示平均成绩Y/N ?");ch=getchar();if(ch=='y'||ch=='Y'){printf("\n 语文数学英语总成绩\n");printf("%-8.2f%-8.2f%-8.2f%-8.2f",a[0],a[1],a[2],a[L-1]);getchar();}break;}case 14:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{xiugai(head);break;}case 15:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{fb_avrage(a);printf("平均值保存成功!");getchar();break;}case 16:if(head==NULL||head->next==NULL){printf("请先录入学生的数据!");getchar();break;}else{k=t_ji_tj(head);if(k>0){printf("符合条件的有%3d 名学生!",k);getchar();}else if(k==0){printf("没有符合条件的学生!");getchar();}else if(k==-2)getchar();break;}default :printf("输入有误,请重新输入!");getchar();break;}}}//输出单个的学生数据void printf_(struct stu *p0){int i;printf("学号:");puts(p0->num);printf("姓名:");puts(p0->name);printf("语文数学英语总成绩\n");for(i=0;i<L;i++)printf("%-8d",p0->ch_ji[i]);printf("\n");}//接收单个的学生数据,返回指针,struct stu * scanf1_(){int i,sum=0;struct stu *p0;p0=(struct stu *)malloc(sizeof(struct stu));printf("请输入学生的学号:");gets(p0->num);printf("请输入学生的姓名:");gets(p0->name);printf("请输入学生的三科成绩(语文、数学、英语):");for(i=0;i<L-1;i++){scanf("%d",&p0->ch_ji[i]);sum+=p0->ch_ji[i];}getchar();p0->ch_ji[L-1]=sum;return p0;}//得到学生的数据,并放入到链表中struct stu * scanf_(){struct stu *p1,*p2,*head;int i,sum=0,n=0;printf("注意:当学生学号为0 时输入结束\n\n");p1=(struct stu *)malloc(sizeof(struct stu)); /*得到学生信息*/head=p2=p1;do{ printf("请输入学生的学号:");gets(p1->num);if(strcmp(p1->num,"0")==0)break;printf("请输入学生的姓名:");gets(p1->name);printf("请输入学生的三科成绩(语文、数学、英语):");for(i=0;i<L-1;i++){scanf("%d",&p1->ch_ji[i]);sum+=p1->ch_ji[i];}getchar();printf("\n");p1->ch_ji[L-1]=sum;sum=0;p1=(struct stu *)malloc(sizeof(struct stu));p2->next=p1;p2=p1;n++;}while(1);p1->next=NULL;printf("成绩输入结束!\n");getchar();/*学生的数据被放在链表中*/return head;}/*输出链表中的学生信息*/void print_(struct stu *head){int i;struct stu *p1,*p2;p1=p2=head;while(p1->next!=NULL){printf("学号:");puts(p1->num);printf("姓名:");puts(p1->name);printf("语文数学英语总成绩\n");for(i=0;i<L;i++)printf("%-8d",p1->ch_ji[i]);printf("\n");p1=p2->next;p2=p1;}getchar();}/*用链表给学生排序*/struct stu *num_paixu(struct stu *head){struct stu *p1,*p2,*p3,*p4,*p5,*p6,*p7;int x=1;for(p1=head;p1->next!=NULL;p4=p1,p1=p1->next){p3=p1;for(p2=p1->next,p5=p7=p2;p2->next!=NULL;p7=p2,p2=p2->next){if(strcmp(p3->num,p2->num)>0){p3=p2;p5=p7;}}if(p3!=p1){if(x&&p1==head){p6=p1->next;p1->next=p3->next;p3->next=p6;p5->next=p1;head=p3;p1=p3;x=0;}else{p6=p1->next;p1->next=p3->next;p3->next=p6;p4->next=p3;p5->next=p1;p1=p3;}}printf("排序成功!\n");return head;}//根据学生的学号查询学生的信息,并把学生的数据输出void chaxun(struct stu *head){char a[N];struct stu *p1;printf("请输入要查询的学号:");gets(a);p1=head;while(p1->next!=NULL){if(strcmp(p1->num,a)==0)break;p1=p1->next;}if(p1->next==NULL)printf("没有找到学号为%s 的学生!\n",a);else{printf("要查询的学生信息如下:\n\n");printf_(p1);}}//将学生的数据插入到顺序排放的链表中,并且插入后也是顺序排放的。
学生成绩管理以单链表作为存储结构,设计和实现某班某门课程成绩管理的完整程序。
程序要求完成如下功能:(1)创建成绩链表,学生数据包含学生的学号、姓名和成绩。
(2)可以在指定学号学生前插入学生成绩数据。
(3)可以删除指定学号的学生数据。
(4)可以计算学生的总数。
(5)可以按学号和姓名查找学生。
(6)可以显示所有学生的成绩。
(7)可以把学生成绩按从高到低的顺序排列。
此处的设计思想基本与顺序表相同,只是对保存学生成绩的线性表采用不同的存储结构实现。
本例中用到的学生数据也是程序运行时由用户从键盘输入,保存到一个单链表中。
学生结构体类型的定义与顺序表应用举例处的定义相同,用C语言描述如下:typedef struct Student /*学生类型定义*/{ int score; /*成绩*/char sno[5],sname[8]; /*学号,姓名*/}Student;当学生的学号为“#”时,也是表示数据输入的结束。
单链表中保存的数据元素均为学生Student类型,则单链表定义如下:typedef struct Node /*结点类型定义*/{ Student studentInfo; /*学生信息*/struct Node *next; /*指向后继元素的指针域*/}LinkList;对学生的成绩按从高到低排序时,使用的也是直接插入排序思想。
此外,为了排序后还能在原单链表上继续进行操作,这里是把单链表中的内容复制到一个新单链表中,对新单链表排序,原单链表不变。
下面是以单链表作为存储结构实现的学生某门课程成绩管理的完整C语言程序。
#include<string.h>#include<malloc.h>#include <stdlib.h>#include <stdio.h>typedef struct Student /*学生类型定义*/{ int score; /*成绩*/char sno[5],sname[8]; /*学号,姓名*/}Student;typedef struct Node /*结点类型定义*/{ Student studentInfo; /*学生信息*/struct Node *next; /*指向后继元素的指针域*/}LinkList;void display(LinkList *p) /*在屏幕上显示一个学生的成绩信息*/{ printf("\n\n\nno\t\tname\t\tscore: ");printf("\n%s",p->studentInfo.sno); /*打印学号*/printf("\t\t ");printf("%s",p->studentInfo.sname); /*打印姓名*/printf("\t\t ");printf("%-4d\n",p->studentInfo.score); /*打印成绩*/}void displayAll(LinkList *L) /*在屏幕上显示所有学生的成绩信息*/ { LinkList *p;p=L->next;printf("\n\n\nno\t\tname\t\tscore: ");while(p){ printf("\n%s",p->studentInfo.sno); /*打印学号*/printf("\t\t ");printf("%s",p->studentInfo.sname); /*打印姓名*/printf("\t\t ");printf("%-4d\n",p->studentInfo.score);/*打印成绩*/p=p->next;}}LinkList *inputdata( ) /*输入学生信息*/{ LinkList *s=NULL ; /*s是指向新建结点的指针*/ char sno[5]; /*存储学号的数组*/printf("\n ");printf(" no: ");scanf("%s",sno); /*输入学号*/if(sno[0]=='#') /*#结束输入*/return s;s=( LinkList *)malloc(sizeof(LinkList));strcpy(s->studentInfo.sno,sno);if(strlen(sno)>4) /*如果sno字符个数大于等于5,因为字符串没有'\0'结束标志,在读数据时将把姓名字符一起读到sno数组,因此做了如下处理*/ s->studentInfo.sno[4]='\0';printf(" name: ");scanf("%s",s->studentInfo.sname); /*输入姓名*/printf("score: ");scanf("%d",&s->studentInfo.score);/*输入成绩*/return s;}LinkList *createTailList( ) /*以尾插法建立带头结点的学生信息单链表*/{ LinkList *L,*s, *r; /*L头指针,r尾指针,s是指向新建结点的指针*/ L=( LinkList *)malloc(sizeof (LinkList)); /*建立头结点,申请结点存储空间*/r=L; /*尾指针指向头结点*/printf("\请输入学生成绩,当学号no为\"#\"时结束:\n\n ");while (1) /*逐个输入学生的成绩*/{ s=inputdata( );if(!s) break; /*s为空时结束输入*/r->next=s; /*把新结点插入到尾指针后*/r=s; /*r 指向新的尾结点*/ }r->next=NULL; /*尾指针的指针域为空*/displayAll(L); /*显示所有学生信息*/return L;}Void locateElemByno(LinkList *L, char ch[5]) /*按学号查找学生的算法*/{ LinkList *p=L->next; /*从第一个结点开始查找*/ while ( p && (strcmp(p->studentInfo.sno,ch)!=0))/*p不空且输入学号与链表中学号不等*/ p = p ->next;if (!p){ printf("\n\n\tDon't find the student!\n" );}else{ display(p); /*显示查找到的学生信息*/}}void locateElemByname(LinkList *L, char sname[8])/*按姓名查找学生的算法*/{ LinkList *p=L->next; /*从第一个结点开始查找*/ while ( p&& (strcmp(p->studentInfo.sname,sname)!=0)) /*p不空且输入姓名与链表中姓名不等*/ p = p ->next;if (!p){ printf("\n\n\tDon't find the student!\n" ); }else{display(p); /*显示查找到的学生信息*/}}int lengthList (LinkList *L) /*求学生总人数的算法*/{ LinkList * p=L->next; /* p指向第一个结点*/int j=0;while (p){ p=p->next; j++ ;} /* p所指的是第j 个结点*/return j;}void insertElem ( LinkList *L, char ch[5]) /*在带头结点的单链表L中指定学号前插入学生*/ { LinkList *p,*s;p=L; /*从头结点开始查找学号为ch的结点的前趋结点p */while ((p->next) && (strcmp(p->next->studentInfo.sno,ch)!=0))p = p ->next;s=inputdata(); /*输入欲插入学生信息*/s->next=p->next;p->next=s;}void deleteElem (LinkList *L, char ch[5]) /*删除给定学号的学生信息的算法*/{ LinkList *p,*q;p=L;while ( (p->next)&&(strcmp(p->next->studentInfo.sno,ch)!=0 )){ p=p->next; /*从头结点开始查找学号为ch的结点的前趋结点p*/}if (!p->next) /* 已经扫描到表尾也没找到*/{ printf("\n\n\tDon't find the student!\n" );}else{ q=p->next; /*q指向学号为ch的结点*/printf("\n\ndeleted student's information:");display(q);p->next=q->next; /*改变指针*/free(q); /*释放q占用空间*/printf("\n\nall student's information :");displayAll(L);}}void insertSort(LinkList *L) /*用直接插入排序思想把学生的成绩按从高到低排序,结果保存在新有序链表中,原链表不变*/{ L inkList *L1,*p; /*L1有序链表的表头,p插入位置前结点*/ LinkList *q,*s; /*q欲插入L1中的结点*/int len;len=lengthList (L) ;L1=( LinkList *)malloc(sizeof (LinkList)); /*建立头结点,申请结点存储空间*/if (L->next) /*链表L非空*/{ /*生成有序链表的第一个结点*/s=( LinkList *)malloc(sizeof (LinkList)); /*建立结点,申请结点存储空间*/strcpy(s->studentInfo .sno ,L->next->studentInfo.sno);strcpy(s->studentInfo .sname,L->next->studentInfo.sname);s->studentInfo .score =L->next->studentInfo.score;s->next =NULL;L1->next=s; /*只有原单链表的第一个结点的有序链表L1*/q=L->next->next; /*原单链表的第二个结点,q即要插入有序链表L1中的结点*/ }else{ printf("\nthe student link list is empty\n");return;}while(q) /*链表L中有结点*/{ p=L1 ; /*从链表L1的第一个结点开始比较*/while((p->next) && (p->next->studentInfo.score>=q->studentInfo.score))p=p->next ; /*查找插入位置前结点*//*生成欲插入有序链表中的结点*/s=( LinkList *)malloc(sizeof (LinkList));/*建立结点,申请结点存储空间*/strcpy(s->studentInfo .sno ,q->studentInfo.sno);strcpy(s->studentInfo .sname ,q->studentInfo.sname);s->studentInfo .score =q->studentInfo.score;if(!p->next) /*p是有序链表的最后一个结点*/{ s->next =NULL ;p->next =s;}else{ s->next =p->next ;p->next =s;}q=q->next; /*下一个欲插入有序链表的结点*/ }/*while(!q)*/displayAll(L1); /*显示生成的有序链表*/}void main(){ printf("=============================================\n\n");printf(" 带头结点的学生成绩管理程序\n\n");printf("=============================================\n\n");LinkList *L;char ch[5],sname[8];int b=1;while(b){ int a;printf("\n\n");printf(" <1>创建(带头尾插)<2>指定学号前插入<3>按学号删除\n ");printf("<4>计算学生总数<5> 按学号查找<6> 按姓名查找\n");printf(" <7>显示所有学生<8>成绩排序<9> 退出\n");printf("\n请输入功能选项:");scanf("%d",&a);switch(a){case 1:L=CreateTailList();break;case 2:printf("\n输入欲在哪个学号前插入数据:");scanf("%s",ch);insertElem(L, ch) ;break;case 3:printf("\n输入欲删除学生的学号:");scanf("%s",ch);deleteElem(L, ch) ;break;case 4:printf(" \n学生总数为:%d \n",lengthList (L) );break;case 5:printf("\n输入欲查找学生的学号:");scanf("%s",ch);locateElemByno(L, ch) ;break;case 6:printf("\n输入欲查找学生的姓名:");scanf("%s",sname);locateElemByname(L, sname );break;case 7:displayAll(L);break;case 8:insertSort(L);break;case 9:printf("\n已退出\n");b=0;break;};}}上机运行程序后,程序执行结果如图2.31(a)~(i)所示。
实验二:学生信息管理(单链表)【实验目的】1. 设计一个学生信息管理系统2. 掌握用C语言定义单链表结构,并实现其创建、插入、删除等基本操作。
【实验内容】本次实验通过单链表的基本操作,实现一个简单的学生信息管理系统,包括:学生信息链表的建立、添加学生信息、查询学生信息、删除学生信息、输出所有学生信息。
【实验要求】本实验是对学生的信息管理作一个简单的模拟,用菜单选择操作方式完成下列功能:链表的建立2.插入学生信息3.查询学生信息4.删除学生信息5.输出所有学生信息0.退出管理系统【知识要点】本实验涉及单链表的各种操作,包括单链表的建立、结点的查找、插入、删除等基本运算。
链表中插入结点的指针变化,删除p所指结点的指针变化。
【实现提示】本题实质是建立学生信息线性表,每条信息由学号、姓名、性别与成绩组成,即链表中每个结点由5个域组成,分别为:学号、姓名、成绩、存放下一个结点地址的next域。
要求完成的五项功能可写成五个函数(0项功能由菜单程序实现),登记学生成绩对应建立学生单链表的功能,2、3、4这三个功能分别对应单链表的插入、查询与删除三大基本操作。
【代码】#include <stdio.h>#include<malloc.h>#include<string.h>typedef struct{char num[8];/*学号*/char name[9];/*姓名*/char gender[3];/*性别*/int score;/*成绩*/}DataType;typedef struct node{DataType data;struct node *next;}ListNode;typedef ListNode *LinkList;LinkList head;/*函数说明*/int menu_select();LinkList createList(void);void printList(LinkList head);int insertNode(LinkList head,ListNode *p,int i);ListNode *findList(LinkList head);void delNode(LinkList head);void main(){ListNode *p;int i;while(1){switch(menu_select()){case 1:printf("**************************************\n");printf(" 学生信息链表的建立\n");printf("***************************************\n");head = createList();break;case 2:printf("**************************************\n");printf("添加学生信息\n");printf("**************************************\n");printf("\n学号(8)姓名(8)性别成绩\n");printf("**************************************\n");p=(ListNode *)malloc(sizeof(ListNode));scanf("%s%s%s%d",p->data.num,p->,p->data.gender,&p->data.score);printf("请输入要插入的位置:\n");fflush(stdin);scanf("%d",&i);if(insertNode(head,p,i)==-1){printf("没有合适的插入点!\n");}else{printf("结点已经插入\n");}break;case 3:printf("**************************************\n");printf("查询学生信息\n");printf("**************************************\n");p=findList(head);if(p!=NULL){printf("\n学号(8)姓名(8)性别成绩\n");printf("-------------------------------------------\n");printf("%s,%s,%s,%d\n",p->data.num,p->,p->data.gender,p->data.score);printf("------------------------------------------------------------------\n");}elseprintf("没查到要查询的学生信息!");break;case 4:printf("**************************************\n");printf("删除学生信息\n");printf("**************************************\n");delNode(head);break;case 5:printf("**************************************\n");printf("输出所有学生信息\n");printf("**************************************\n");printList(head);break;case 0:printf("再见!\n");getchar();return;}}}int menu_select(){int sn;printf("\n 学生信息管理系统\n");printf("=========================================\n");printf(" 1.学生信息链表的建立\n");printf(" 2.插入学生信息\n");printf(" 3.查询学生信息\n");printf(" 4.删除学生信息\n");printf(" 5.输出所有学生信息\n");printf(" 0.退出管理系统\n");printf("==========================================\n");printf("请选择0-5:\n");for(;;){scanf("%d",&sn);if (sn<0 || sn>5)printf("\n\t输入错误,重选0-5\n");elsebreak;}return sn;}LinkList createList(void){ListNode *p,*rear;char flag = 'y';head = (ListNode *)malloc(sizeof(ListNode));rear = head;while(flag=='y' || flag=='Y'){p=(ListNode *)malloc(sizeof(ListNode));printf("\n学号(8)姓名(8)性别成绩\n");scanf("%s%s%s%d",p->data.num,p->,p->data.gender,&p->data.score);rear->next = p;rear = p;printf("继续输入吗?(y/n):");flag = getchar();}rear->next = NULL;return head;}void printList(LinkList head){ListNode *p;p=head->next;printf("\n学号(8)姓名(8)性别成绩\n");printf("-------------------------------------------\n");while(p!=NULL){printf("%s,%s,%s,%d\n",p->data.num,p->,p->data.gender,p->data.score);printf("------------------------------------------------------------------\n");p=p->next;}}int insertNode(LinkList head,ListNode *p,int i){ListNode *p1;int j=1;p1=head;if(p1->next==NULL)/*空表:插入作为第一个结点*/{if(i==0){p1->next=p;p->next=NULL;}elsereturn -1;}while((j<=i-1)&&(p1!=NULL))/*找到第i-1个结点,p1指向该结点*/ {p1=p1->next;j++;}if(p1==NULL)/*没有合适的插入点*/return -1;p->next=p1->next;p1->next=p;return 0;}ListNode *findList(LinkList head){ListNode *p;char num[8];char name[9];int xz;printf("===========================\n");printf("1、按学号查询\n");printf("2、按姓名查询\n");printf("===========================\n");printf(" 请选择:");p=head->next;scanf("%d",&xz);if (xz==1){printf("请输入要查找学生的学号:");scanf("%s",num);while(p && strcmp(p->data.num,num)!=0)p=p->next;}elseif (xz==2){printf("请输入要查找学生的姓名:");scanf("%s",name);while (p && strcmp(p->,name)!=0)p=p->next;}return p;}void delNode(LinkList head){ListNode *p,*q;printf("请先查找您要删除的学生信息:\n");p=findList(head);if(p==NULL){printf("没有查到要删除的学生信息");return;}q=head;while(q!=NULL && q->next!=p) q=q->next;q->next=p->next;free(p);printf("该学生信息已被删除!\n");}。
链表实现学⽣管理系统makefilemain : main.o stu.o llist.ogcc -Wall -o $@ $^clean :rm -rf main *.o头⽂件llist.h#ifndef __LLIST_H#define __LLIST_Henum a{HEADINSERT,TAILINSERT};typedef void (llist_print)(const void *data);typedef int (llist_cmp)(const void *data, const void *cp);typedef void LLIST;LLIST *llist_handler(int size);int llist_insert(LLIST *h, const void *data, int mode);int llist_delect(LLIST *h, const void *data, llist_cmp cmp);void *llist_find(LLIST *h, const void *data, llist_cmp cmp);void *llist_display(LLIST *h, llist_print print);void *llist_destroy(LLIST *h);int llist_fetch(LLIST *handler, void *b_data, llist_cmp cmp, void *save);int llist_output(LLIST *h, void *save);#endifstu.h#ifndef __STU_H#define __STU_H#include "llist.h"struct stu{int id;char name[20];long int tel;};void insert_data(LLIST *handler);int delect_data(LLIST *handler);int fetch_data(LLIST *handler);int find_data(LLIST *handler);void display_data(LLIST *handler);void sort_data(LLIST *handler);int save_data(LLIST *handler);int read_data(LLIST *handler);#endifllist.c#include <stdio.h>#include <stdlib.h>#include <string.h>#include "llist.h"struct llist_node{struct llist_node *prev;struct llist_node *next;char data[0];};struct head_node{int size;struct llist_node node;};LLIST *llist_handler(int size){struct head_node *handler = NULL;handler = malloc(sizeof(LLIST));if(handler == NULL)return NULL;handler->size = size;handler->node.prev = handler->node.next = &handler->node; return handler;}int llist_insert(LLIST *h, const void *data, int mode){struct head_node *handler = h;struct llist_node *newnode = NULL;struct llist_node *p = &handler->node;newnode = malloc(sizeof(struct llist_node) + handler->size); if(newnode == NULL)return -1;memcpy(newnode->data, data, handler->size);switch(mode){case HEADINSERT : break;case TAILINSERT : p = p->prev;break;default : free(newnode);break;}newnode->next = p->next;newnode->prev = p->next->prev;newnode->prev->next = newnode;newnode->next->prev = newnode;return0;}struct llist_node *_find(LLIST *h, const void *data, llist_cmp cmp){struct head_node *handler = h;struct llist_node *cur = NULL;for(cur = handler->node.next; cur != &handler->node; cur = cur->next) {if(cmp(cur->data, data))return cur;}return NULL;}void *llist_find(LLIST *h, const void *data, llist_cmp cmp){struct head_node *handler = h;struct llist_node *find = NULL;find = _find(handler, data, cmp);if(NULL == find)return NULL;return find->data;}int llist_delect(LLIST *h, const void *data, llist_cmp cmp){struct head_node *handler = h;struct llist_node *find = NULL;find = _find(handler, data, cmp);if(NULL == find)return -1;find->prev->next = find->next;find->next->prev = find->prev;free(find);}void *llist_display(LLIST *h, llist_print print){struct head_node *handler = h;struct llist_node *cur = handler->node.next;while(cur != &handler->node){print(cur->data);cur = cur->next;}}void *llist_destroy(LLIST *h){struct head_node *handler = h;struct llist_node *p = NULL;struct llist_node *cur = NULL;for(p = handler->node.next; p != &handler->node; p = p->next) {cur = p;p->next->prev = p->prev;p->prev->next = p->next;free(p);p = cur;}free(handler);}int llist_fetch(LLIST *h, void *b_data, llist_cmp cmp, void *save) {struct head_node *handler = h;struct llist_node *cur = NULL;struct llist_node *find = NULL;find = _find(handler, b_data, cmp);if(NULL == find)return -1;memcpy(save, find->data, handler->size);find->next->prev = find->prev;find->prev->next = find->next;free(find);}int llist_output(LLIST *h, void *save){struct head_node *handler = h;struct llist_node *cur = handler->node.next;if(cur == &handler->node)return -1;cur->next->prev = cur->prev;cur->prev->next = cur->next;memcpy(save, cur->data, handler->size);free(cur);}stu.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include "stu.h"static int a, ret, b, bnum, num;static char name[20];static char bname[20];static long int tel, lbnum, lnum;void print(const void *data) //打印函数{const struct stu *p = data;printf("%d %s %ld\n", p->id, p->name, p->tel);}int id_cmp(const void *data, const void *cp) //id对⽐函数{const struct stu *p = data;const int *cur = cp;return !(p->id - *cur);}int tel_cmp(const void *data, const void *cp){const struct stu *p = data;const long int *cur = cp;return !(p->tel - *cur);}int name_cmp(const void *data, const void *cp) //名字对⽐函数{const struct stu *p = data;const char *cur = cp;return !(strcmp(cur, p->name));}void insert_data(LLIST *handler) //增加数据函数{struct stu data;struct stu *find = NULL;system("clear");printf("请输⼊学号:\n");scanf("%d", &num);find = llist_find(handler, &num, id_cmp);if(find != NULL){printf("该学号已存在,请核对后从新输⼊!\n");sleep(2);return ;}data.id = num;printf("请输⼊学⽣姓名:\n");scanf("%s", name);memcpy(, name, 20);printf("请输⼊学⽣电话:\n");scanf("%ld", &tel);data.tel = tel;ret = llist_insert(handler, &data, HEADINSERT);if(ret < 0)printf("未能成功添加学⽣信息。
附录1:结构化设计源程序清单// 程序名称:Student.CPP// 程序功能:采用链表与文件实现一个简单的学生成绩管理系统。
// 程序作者:李卉// 最后修改日期:2008-9-10#include <iostream>#include <fstream>#include<cstring>#include<conio.h>#include <ctime>using namespace std;struct Class //结构体结构成员{ int Chinese;int Math;int English;};class Student{public:Student();void Ofile(ofstream &of); //从文件中提取数据void Infile(ifstream &f); //将数据输入到文件void Out();void Set(char *name,int no,Class score);char *GetName();int GetNo();Student *Next;protected:char Name[20];int No;Class Score ;};Student::Student():Next(0){} // 构造函数char *Student::GetName(){return Name;}int Student::GetNo(){return No;}void Student::Set(char *name,int no,Class score){ strcpy(Name,name);No=no;Score=score;}void Student::Infile(ifstream &f){ f>>Name>>No>>Score.Chinese>>Score.Math>>Score.English; //将数据输入到文件}void Student::Ofile(ofstream &of){ of<<" "<<Name<<" "<<No<<" "<<Score.Chinese<<" "<<Score.Math<<" "<<Score.English; //从文件中提取数据}void Student::Out(){ cout<<Name<<"\t"<<No<<"\t"<<Score.Chinese<<"\t\t"<<Score.Math<<"\t\t"<<Score.English <<"\t"<<endl;}class Function //功能类{public:Function(); //构造函数~Function(); //析构函数void Menu(); //菜单函数void Add(); //录入学生成绩函数void Search(); //查询学生成绩函数void Delete(); //删除学生成绩函数void Modify(); //修改学生成绩函数void Show(); //显示学生成绩函数private:Student *Student_First;void Read(); //读取学生成绩函数void Save(); //保存学生成绩信息函数};Function::Function(){ Student_First=new Student;Read();}Function::~Function(){ delete Student_First;}void Function::Add() //录入学生成绩信息函数{ char name[20];int no;Class score;char choose;Student *f1,*p,*f2;system("cls");f1=Student_First;f2=Student_First->Next;while(f1->Next)f1=f1->Next;do{ p=new Student;cout<<"请输入您要添加的学生成绩信息:"<<endl;cout<<"请输入学生姓名:";cin>>name;while(f2){ if(strcmp(f2->GetName(),name)==0){ cout<<"该学生已存在,请确定姓名!\n\n";cout<<"请输入姓名:";cin>>name;break;}f2=f2->Next;}cout<<"请输入学号:";cin>>no;cout<<"请输入语文成绩:";cin>>score.Chinese;cout<<"请输入数学成绩:";cin>>score.Math;cout<<"请输入英语成绩:";cin>>score.English;p->Set(name,no,score);f1->Next=p; //指针跳到下一链表p->Next=NULL;f1=f1->Next;cout<<"是否继续输入信息?(Y\\N) "<<endl;cin>>choose;}while(choose=='y'||choose=='Y');Save();cout<<"1.返回主菜单"<<endl;cin>>choose;while(choose!='1'){ cout<<"1.返回主菜单"<<endl;cin>>choose;}Menu();//菜单函}void Function::Delete() //删除信息函数{ char name[20];int no;char choose;Student *temp,*p;system("cls");p=temp=Student_First->Next;cout<<"请输入姓名:";cin>>name;cout<<"输入学号:";cin>>no;while(temp)//temp用于存放从键盘输入的字符{ if(strcmp(temp->GetName(),name)==0&&temp->GetNo()==no) //判断该学生信息是否存在{ cout<<"姓名\t学号\t语文成绩\t数学成绩\t英语成绩\n";temp->Out();cout<<"\n是否删除(Y/N)";cin>>choose;if(choose=='y'||choose=='Y'){ p->Next=temp->Next;delete temp;cout<<"删除成功:\n";}break;}p=temp;temp=temp->Next;}Save();cout<<"1.返回主菜单\n2.继续删除"<<endl;cin>>choose;while(choose!='1'&&choose!='2'){ cout<<"1.返回主菜单\n2.继续删除"<<endl;cin>>choose;}if(choose=='1')Menu();else if(choose=='2')Delete();}void Function::Modify() //修改学生信息函数{ char choose,name[20];Student *temp,*p;int no;Class score;system("cls");temp=p=Student_First;cout<<"请输入您要修改的学生姓名:";cin>>name;while(temp){ if(strcmp(temp->GetName(),name)==0){ cout<<"姓名\t学号\t语文成绩\t数学成绩\t英语成绩\n";temp->Out();cout<<"请输入姓名:";cin>>name;cout<<"请输入学号:";cin>>no;cout<<"请输入语文成绩:";cin>>score.Chinese;cout<<"请输入数学成绩:";cin>>score.Math;cout<<"请输入英语成绩:";cin>>score.English;temp->Set(name,no,score);break;}temp=temp->Next;}Save();cout<<"修改成功!"<<endl;cout<<"1.返回主菜单\n2.继续修改"<<endl;cin>>choose;while(choose!='1'&&choose!='2'){ cout<<"1.返回主菜单\n2.继续修改"<<endl;cin>>choose;}if(choose=='1')Menu();else if(choose=='2')Modify();}void Function::Read() //读取信息函数{ Student *p,*p2;p=Student_First;long t;ifstream is("Student.txt",ios::in);if(!is){ ofstream os("Student.txt",ios::out);os.close();return ;}while(!is.eof()){ p2=new Student;p2->Infile(is);p->Next=p2;p2->Next=NULL;p=p->Next;}}void Function::Save() //保存学生成绩信息函数{ ofstream of("Student.txt",ios::out);Student *p=Student_First->Next;while(p){ p->Ofile(of);p=p->Next;}of.close();}void Function::Search(){ int flag(0);char choose;char t1[20];int t2;system("cls");Student *temp=Student_First->Next;do{ cout<<"输入查询方式:\n1.按姓名查询\n2.按学号查询\n";cin>>choose;if(choose=='1'){ cout<<"请输入您要查询的姓名:";cin>>t1;while(temp){ if(strcmp(t1,temp->GetName())==0){ flag=1;break;}temp=temp->Next;}if(flag==0)cout<<"\n无该学生的信息\n"<<endl;else{ cout<<"姓名\t学号\t语文成绩\t数学成绩\t英语成绩\n";temp->Out();}break;}else if(choose=='2'){ cout<<"请输入您要查询的学号";cin>>t2;while(temp){ if(t2==temp->GetNo()){ flag=1;break;}temp=temp->Next;}if(flag==0)cout<<"\n无该学生的信息\n"<<endl;else{ cout<<"姓名\t学号\t语文成绩\t数学成绩\t英语成绩\n";temp->Out();}break;}}while(choose!='1'||choose!='2');cout<<"\n1.返回主菜单\n2.继续查询"<<endl;cin>>choose;while(choose!='1'&&choose!='2'){ cout<<"1.返回主菜单\n2.继续查询"<<endl;cin>>choose;}if(choose=='1')Menu();else if(choose=='2')Search();}void Function::Show(){ char choose;Student *temp;system("cls");temp=Student_First->Next;if(!temp){ cout<<"文件无数据\n\n "<<endl;cout<<"1.返回主菜单"<<endl;cin>>choose;while(choose!='1'){ cout<<"1.返回主菜单"<<endl;cin>>choose;}Menu();}else{ cout<<"姓名\t学号\t语文成绩\t数学成绩\t英语成绩\n";while(temp!=NULL){ temp->Out();temp=temp->Next;}}cout<<"1.返回主菜单"<<endl;cin>>choose;while(choose!='1'){ cout<<"1.返回主菜单"<<endl;cin>>choose;}Menu();}void Function::Menu(){ time_t t;time(&t);char choose;system("cls");cout<<" ------------------------版权所有:李卉-------------------------"<<endl;cout<<endl;cout<<"***********************************************************"<<endl;cout<<" 长沙理工大学欢迎你"<<endl<<endl;cout<<" 学生成绩信息管理系统"<<endl<<endl;cout<<" 显示系统时间和日期: "<<ctime(&t)<<endl;cout<<"*********************************************************"<<endl<<endl;cout<<"请选择您需要的操作,选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl;cout<<"\n";cout<<" 1 录入学生成绩信息:\n"<<endl;cout<<" 2 查询学生成绩信息:\n"<<endl;cout<<" 3 删除学生成绩信息:\n"<<endl;cout<<" 4 修改学生成绩信息:\n"<<endl;cout<<" 5 显示全部学生成绩信息:\n"<<endl;cout<<" 6 退出系统"<<endl;cout<<"\n";cin>>choose;switch(choose){ case '1': Add();break;case '2': Search();break;case '3': Delete();break;case '4': Modify();break;case '5': Show();break;case '6': exit(1);break;default:{ cout<<"请按规定输入选择项!"<<endl;Menu();}}}void main(){ Function function; //定义功能接口function.Menu(); //调用主菜单}。
河北工业大学计算机软件技术基础(VC)课程设计报告一、题目:利用单项链表实现简单的学生信息管理(07)二、设计思路1、总体设计1)分析程序的功能创建单项链表保存学生的各项信息,学号、姓名、成绩。
并能够完成学生信息的插入、删除及信息的显示功能。
2)系统总体结构:按照程序要求的功能采用结构化的设计思想,划分为五个功能模块,即创建链表、插入函数、删除函数、显示函数和主函数。
2、各功能模块的设计:说明各功能模块的实现方法①头文件:对自己定义的函数进行函数声明。
②主函数:进行函数的调用,实现各函数的功能,达到预期的目的。
③函数定义部分:定义各个功能函数,创建链表函数、插入新信息函数、删除信息函数、显示信息函数。
3、设计中的主要困难及解决方案1)在插入新信息时,有插入点在表头、中间、表尾三种情况,为此采用讨论的方法,把三种情况进行讨论使其分开进行。
2)在删除信息时,有删除的为头结点和中间结点的情况,采用讨论的方法,把两种情况分开来进行。
4、你所设计的程序最终完成的功能1)创建链表、插入新信息、删除信息、显示信息。
2)测试数据①输入的数据99812 LiuLifang 91学号姓名成绩96085 WangLiPing 7798120 ZhangLi 7599912 LiuHai 80 ③删除的数据学号姓名成绩99812 liulifang 91运行结果三、程序清单本程序包含creatlist.cpp、insert.cpp、del.cpp、output.cpp、main.cpp、头文件.h六个文件1、creatlist.cpp文件清单#include<iostream.h>#include"头文件.h"int n;student *creatlist(){student *head;student *p1;student *p2;n=0;head=NULL;p1=new(student); //创建一个新结点p2=p1;cin>>p1->num>>p1->name>>p1->score;while(p1->num!=0) //链表建立过程结束的判断条件{n++;if(n==1) //将链表中第一个新建结点作为表头head=p1;elsep2->next=p1; //原链表结点指向新建结点p2=p1;p1=new(student);cin>>p1->num>>p1->name>>p1->score;}delete(p1);p2->next=NULL;return head; //返回表头}2、insert.cpp文件清单#include<iostream.h>#include"头文件.h"student *insert(student *head,student *t){student *p0; //待插入点student *p1;student *p2; //p0插入p1之前,p2之后p1=head;p0=t;if(p1==NULL) //原链表是空表{head=p0;p0->next=NULL;}elsewhile((p0->num>p1->num)&&(p1->next!=NULL)) //查找待插入点{p2=p1;p1=p1->next;}if(p0->num<=p1->num){if(p1==head) //要插入的位置在表头{head=p0;p0->next=p1;}else //要插入的位置不是表头{p2->next=p0;p0->next=p1;}}else //插入表尾结点之后{p1->next=p0;p0->next=NULL;}}return head; //返回表头}3、del.cpp文件清单#include<iostream.h>#include"头文件.h"student *del(student *head,int num){student *p1;student *p2;if(head==NULL) //原链表是空表{cout<<"List is NULL\n";return head;}p1=head;while((num!=p1->num)&&(p1->next!=NULL)) //查找带要删除的结点{p2=p1;p1=p1->next;if(num==p1->num){if(p1==head) //要删除的是头结点head=p1->next;else //要删除的不是头结点p2->next=p1->next;delete(p1); //释放被删除结点所占的内存空间cout<<"delete: "<<num<<endl;}elsecout<<"not found"<<endl;return head; //返回表头}4、output.cpp文件清单#include<iostream.h>#include"头文件.h"void output(student *head){if(head==NULL) //原链表是空表cout<<"list is NULL\n";else{student *p1;p1=head;cout<<"学生的成绩信息"<<endl;cout<<"学号"<<"\t姓名"<<"\t成绩\n";do //输出链表中各个同学的信息{cout<<p1->num<<"\t"<<p1->name<<"\t"<<p1->score<<endl;p1=p1->next;}while(p1!=NULL);}}5、main.cpp文件清单#include<iostream.h>#include"头文件.h"void main(){student *headl;cout<<"输入学生的成绩信息"<<endl;cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;headl=creatlist();int k;while(1) //菜单选项{cout<<endl;cout<<"--------菜单选项---------"<<endl;cout<<"1.插入新信息 ,请选择:1"<<endl;cout<<"2.删除信息, 请选择:2"<<endl;cout<<"3.显示信息, 请选择:3"<<endl;cout<<"4.结束程序, 请选择:4"<<endl;cout<<" 选择";cin>>k;if(k==1) //插入新信息{int m;cout<<"输入插入学生人数"<<endl;cin>>m;cout<<"学号"<<"\t姓名"<<"\t成绩"<<endl;for(int i=0;i<m;i++){student *stu;stu=new(student);cin>>stu->num>>stu->name>>stu->score;headl=insert(headl,stu);}}else if(k==2) //删除信息{int num;cout<<"输入要删除学生的学号\n";cin>>num;headl=del(headl,num);}else if(k==3) //显示信息{output(headl);}else //结束程序break;};}6、头文件.h文件清单struct student //定义结构体类型{int num;char name[20];double score;student *next;};student *creatlist(); //创建链表函数原型说明student *insert(student * ,student * ); //插入函数原型说明student *del(student * ,int ); //删除函数原型说明void output(student * ); //显示函数原型说明四、对该设计题目有何更完善的方案1、对自己完成程序进行自我评价。
完成了课程设计的基本要求,同时在此基础上进行了一些创新,使用了多文件,使程序看起来更清晰更有条理。
但由于能力有限,以及对C++的认识不深,其中还有不够完善合理的地方。
2、对课题提出更完善的方案增加按照成绩对链表进行排序的功能,使学生信息能够按照成绩的高低进行显示,能够更清晰地显示学生的学习情况五、收获及心得体会1、通过本次课程设计,自己在哪些方面的能力有所提高。
加深了对利用C++语言进行程序设计的理解,提高了对函数的运用能力,提高了软件系统分析能力和使用多文件、归纳总结的能力。
2、收获和心得体会。
通过自己对单项链表的学习,熟悉了链表的建立、插入、删除等操作方法。
通过这次课程设计使我明白了自学的重要性,有了一些自学的学习方法和技巧,并且要积极的与其他同学共同讨论,在讨论中才能找到自己认识的不足,改正自己的错误。
日期:20XX年6月20日。