C語言嵌套鏈表實現學生成績管理系統(tǒng)
C語言嵌套鏈表實現學生成績管理系統(tǒng),供大家參考,具體內容如下
鏈表A,每個節(jié)點存放一個新的鏈表B1,B2,B3,B4,B5的頭結點。 場景: 一個年級,相當鏈表A 該年級5個班,每個班5個人,相當于鏈表B1--B5 做一個學生成績管理系統(tǒng) 學生成績有語文 數學 英語 功能: 錄入成績 找三科總分的最高分 最低分 算出平均分
前言
鏈表是一種常見的基礎數據結構,結構體指針在這里得到了充分的利用。
鏈表可以動態(tài)的進行存儲分配,也就是說,鏈表是一個功能極為強大的數組,他可以在節(jié)點中定義多種數據類型,還可以根據需要隨意增添,刪除,插入節(jié)點。
鏈表都有一個頭指針,一般以head來表示,存放的是一個地址。鏈表中的節(jié)點分為兩類,頭結點和一般節(jié)點,頭結點是沒有數據域的。鏈表中每個節(jié)點都分為兩部分,一個數據域,一個是指針域。
說到這里你應該就明白了,鏈表就如同車鏈子一樣,head指向第一個元素:第一個元素又指向第二個元素;……,直到最后一個元素,該元素不再指向其它元素,它稱為“表尾”,它的地址部分放一個“NULL”(表示“空地址”),鏈表到此結束。
作為有強大功能的鏈表,對他的操作當然有許多,比如:鏈表的創(chuàng)建,修改,刪除,插入,輸出,排序,反序,清空鏈表的元素,求鏈表的長度等等。
C語言嵌套鏈表實現學生成績管理系統(tǒng):熟悉鏈表的創(chuàng)建,結構體指針的使用。
實現思路:創(chuàng)建學生鏈表->創(chuàng)建班級鏈表
其中
學生鏈表的結點的數據域存放學生的信息;
班級鏈表的結點的數據域為指向學生鏈表頭結點的指針;
利用這樣的嵌套鏈表實現多個班級,以及每個班級多個學生的成績管理。
提示:以下是本篇文章正文內容,下面案例可供參考
一、代碼實現
1.包含頭文件
代碼如下(示例):
#include<stdio.h> #include<stdlib.h>
2.定義學生鏈表的結點
代碼如下(示例):
struct Student //聲明學生鏈表的節(jié)點
{
? ? ? ? int chinese;
? ? ? ? int math;
? ? ? ? int english;
? ? ? ? int sum;
? ? ? ? struct Student* next;
};3.定義班級鏈表的結點
代碼如下(示例):
struct Class//聲明班級鏈表的節(jié)點
{
? ? ? ? struct Student* student;
? ? ? ? struct Class* next;
};4.創(chuàng)建一個新的學生鏈表的結點并且通過尾插法插入鏈表中
代碼如下(示例):
struct Student* CreateStudentNode(struct Student*head,int num)//生成一個新的學生節(jié)點并且利用尾插法插入鏈表中
{
? ? ? ? struct Student* p=NULL;
? ? ? ? struct Student* node=(struct Student*)malloc(sizeof(struct Student));//為新節(jié)點開辟空間
? ? ? ? //初始化新節(jié)點
? ? ? ? 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){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //鏈表只有一個節(jié)點時
? ? ? ? ? ? ? ? head->next=node;
? ? ? ? ? ? ? ? return head;
? ? ? ? }
? ? ? ? else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //鏈表有多個節(jié)點時 ?將指針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));//生成頭節(jié)點
? ? ? ? struct Student* p=NULL;
? ? ? ? scanf("%d",&sum);//學生數
? ? ? ? for(i=0;i<sum;i++){
? ? ? ? ? ? ? ? p=CreateStudentNode(head,i);
? ? ? ? }
? ? ? ? return p;
}6.創(chuàng)建一個新的班級鏈表的結點并且通過尾插法插入鏈表中
代碼如下(示例):
struct Class* CreateClassNode(struct Class* head,int num)//生成一個新的班級節(jié)點并且利用尾插法插入鏈表中
{
? ? ? ? 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)//打印節(jié)點信息
{
? ? ? ? int max,min;
? ? ? ? struct Class *q=NULL;
? ? ? ? struct Student *p=NULL;
? ? ? ? q=head->next;
? ? ? ? min=max=q->student->next->sum;
? ? ? ? printf("*****************************************************************************************************\n");
? ? ? ? printf("成績統(tǒng)計\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;
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
教你Visual?Studio?2022如何新建一個C語言工程(圖文詳解)
這篇文章主要介紹了Visual?Studio?2022如何新建一個C語言工程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
C++ STL 內 std::{bind/tuple/function} 簡單實現
這篇文章主要介紹了C++ STL 內 std::{bind/tuple/function} 簡單實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02

