C语言嵌套链表实现学生成绩管理系统,供大家参考,具体内容如下
链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。 场景: 一个年级,相当链表A 该年级5个班,每个班5个人,相当于链表B1--B5 做一个学生成绩管理系统 学生成绩有语文 数学 英语 功能: 录入成绩 找三科总分的最高分 最低分 算出平均分
前言
链表是一种常见的基础数据结构,结构体指针在这里得到了充分的利用。
链表可以动态的进行存储分配,也就是说,链表是一个功能极为强大的数组,他可以在节点中定义多种数据类型,还可以根据需要随意增添,删除,插入节点。
链表都有一个头指针,一般以head来表示,存放的是一个地址。链表中的节点分为两类,头结点和一般节点,头结点是没有数据域的。链表中每个节点都分为两部分,一个数据域,一个是指针域。
说到这里你应该就明白了,链表就如同车链子一样,head指向第一个元素:第一个元素又指向第二个元素;……,直到最后一个元素,该元素不再指向其它元素,它称为“表尾”,它的地址部分放一个“NULL”(表示“空地址”),链表到此结束。
作为有强大功能的链表,对他的操作当然有许多,比如:链表的创建,修改,删除,插入,输出,排序,反序,清空链表的元素,求链表的长度等等。
C语言嵌套链表实现学生成绩管理系统:熟悉链表的创建,结构体指针的使用。
实现思路:创建学生链表->创建班级链表
其中
学生链表的结点的数据域存放学生的信息;
班级链表的结点的数据域为指向学生链表头结点的指针;
利用这样的嵌套链表实现多个班级,以及每个班级多个学生的成绩管理。
提示:以下是本篇文章正文内容,下面案例可供参考
一、代码实现
1.包含头文件
代码如下(示例):
- #include<stdio.h>
- #include<stdlib.h>
2.定义学生链表的结点
代码如下(示例):
- struct Student //声明学生链表的节点
- {
- ? ? ? ? int chinese;
- ? ? ? ? int math;
- ? ? ? ? int english;
- ? ? ? ? int sum;
- ? ? ? ? struct Student* next;
- };
3.定义班级链表的结点
代码如下(示例):
- struct Class//声明班级链表的节点
- {
- ? ? ? ? struct Student* student;
- ? ? ? ? struct Class* next;
- };
4.创建一个新的学生链表的结点并且通过尾插法插入链表中
代码如下(示例):
- struct Student* CreateStudentNode(struct Student*head,int num)//生成一个新的学生节点并且利用尾插法插入链表中
- {
- ? ? ? ? struct Student* p=NULL;
- ? ? ? ? struct Student* node=(struct Student*)malloc(sizeof(struct Student));//为新节点开辟空间
- ? ? ? ? //初始化新节点
- ? ? ? ? node->next=NULL;
- ? ? ? ? printf("输入第%d个学生的信息:(语文 数学 英语)\n",num+1);
- ? ? ? ? scanf("%d %d %d",&node->chinese,&node->math,&node->english);
- ? ? ? ? node->sum=node->chinese+node->math+node->english;
-
- ? ? ? ? if(head->next==NULL){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //链表只有一个节点时
- ? ? ? ? ? ? ? ? head->next=node;
- ? ? ? ? ? ? ? ? return head;
- ? ? ? ? }
- ? ? ? ? else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //链表有多个节点时 ?将指针p移到链表尾 ?
- ? ? ? ? ? ? ? ? p=head;
- ? ? ? ? ? ? ? ? while(p->next!=NULL){ ? ?p=p->next; ?} ? ? ?//将指针p移到链表尾 ? ??
- ? ? ? ? }
-
- ? ? ? ? p->next=node;
- ? ? ? ? return head;
- }
5.生成学生链表
代码如下(示例):
- struct Student* init_StudentLink()//生成学生链表
- {
- ? ? ? ? int sum,i;
- ? ? ? ? struct Student* head=(struct Student*)malloc(sizeof(struct Student));//生成头节点
- ? ? ? ? struct Student* p=NULL;
- ? ? ? ? scanf("%d",&sum);//学生数
- ? ? ? ? for(i=0;i<sum;i++){
-
- ? ? ? ? ? ? ? ? p=CreateStudentNode(head,i);
-
- ? ? ? ? }
-
- ? ? ? ? return p;
- }
6.创建一个新的班级链表的结点并且通过尾插法插入链表中
代码如下(示例):
- struct Class* CreateClassNode(struct Class* head,int num)//生成一个新的班级节点并且利用尾插法插入链表中
- {
- ? ? ? ? struct Class* p=NULL;
- ? ? ? ? struct Class* node=(struct Class*)malloc(sizeof(struct Class));
- ? ? ? ? node->next=NULL;
- ? ? ? ? struct Student* q=NULL;
-
- ? ? ? ? printf("输入第%d班级的人数:\n",num+1);
- ? ? ? ? q=init_StudentLink();
- ? ? ? ? node->student=q;
-
- ? ? ? ? if(head->next==NULL){
- ? ? ? ? ? ? ? ? head->next=node;
- ? ? ? ? }
- ? ? ? ? else{
- ? ? ? ? ? ? ? ? p=head;
- ? ? ? ? ? ? ? ? while(p->next!=NULL){ p=p->next; }
-
- ? ? ? ? }
-
- ? ? ? ? return 0;
-
- }
7.生成班级链表
代码如下(示例):
- void init_Class(struct Class* head)//生成班级链表
- {
- ? ? ? ? int sum,i;
- ? ? ? ? printf("请输入建立的班级数\n");
- ? ? ? ? scanf("%d",&sum);
- ? ? ? ? for(i=0;i<sum;i++){
- ? ? ? ? ? ? ? ? CreateClassNode(head,i);
- ? ? ? ? }
-
- }
8.打印结点信息
代码如下(示例):
- void printf_node(struct Class *head)//打印节点信息
- {
- ? ? ? ? int max,min;
- ? ? ? ? struct Class *q=NULL;
- ? ? ? ? struct Student *p=NULL;
- ? ? ? ? q=head->next;
- ? ? ? ? min=max=q->student->next->sum;
- ? ? ? ? printf("*****************************************************************************************************\n");
- ? ? ? ? printf("成绩统计\t(语文\t数学\t英语\t总分\t平均分)\n");
- ? ? ? ? printf("*****************************************************************************************************\n");
-
- ? ? ? ? int i=0,j=0;
- ? ? ? ? p=q->student->next;
- ? ? ? ? while(q){
- ? ? ? ? ? ? ? ? i++;
- ? ? ? ? ? ? ? ? for(p;p->next!=NULL;p=p->next){
- ? ? ? ? ? ? ? ? ? ? ? ? j++;
- ? ? ? ? ? ? ? ? ? ? ? ? printf("第%d班第%d学生的成绩\n",i,j);
- ? ? ? ? ? ? ? ? ? ? ? ? printf("语文:%d 数学:%d ?英语:%d 总分:%d 平均分:%lf\n",p->chinese,p->math,p->english,p->sum,(double)(p->sum)/3);
- ? ? ? ? ? ? ? ? ? ? ? ? if(p->sum>max){
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? max=p->sum;
- ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? ? ? ? ? if(p->sum<min){
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? min=p->sum;
- ? ? ? ? ? ? ? ? ? ? ? ? }
-
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? printf("\n");
- ? ? ? ? ? ? ? ? q=q->next;
- ? ? ? ? ? ? ? ? j=0;
- ? ? ? ? }
-
- ? ? ? ? printf("总分最高为:%d\n",max);
- ? ? ? ? printf("总分最低为:%d\n",min);
- }
9,主函数
代码如下(示例):
- int main()
- {
-
- ? ? ? ? struct Class* head=(struct Class*)malloc(sizeof(struct Class));
- ? ? ? ? head->next=NULL;//生成班级头结点
- ? ? ? ? init_Class(head);//生成班级链表
- ? ? ? ? printf_node(head);//打印信息
-
- ? ? ? ? return 0;
- }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。