C語言實現(xiàn)學生信息管理系統(tǒng)(鏈表)
更新時間:2022年06月20日 11:07:48 作者:Demo龍
這篇文章主要為大家詳細介紹了C語言實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)學生信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
1.頭文件和預處理
#include <stdio.h> #include<string.h> #include<malloc.h> #include<stdlib.h> #include<stdbool.h> #include<iostream> using namespace std; #define NO_LENGTH ?20 #define NAME_LENGTH 11
2.定義學生結(jié)構(gòu)體的數(shù)據(jù)結(jié)構(gòu)
typedef struct Student{ ?? ?char studentNo[NO_LENGTH]; ?? ?char studentName[NAME_LENGTH]; ?? ?int score; }st;
3.定義每條記錄或節(jié)點的數(shù)據(jù)結(jié)構(gòu)
/* 定義每條記錄或節(jié)點的數(shù)據(jù)結(jié)構(gòu) */ typedef struct node { ?? ?struct Student data; //數(shù)據(jù)域 ?? ?struct node *next; //指針域 }Node,*Link; ?//Node為node類型的別名,Link為node類型的指針別名
4.函數(shù)接口代碼.
1).定義提示菜單
//定義提示菜單 void myMenu(){ ?? ?printf("*****************************菜單*****************************\n");? ?? ?printf("***********************1 增加學生記錄*************************\n");? ?? ?printf("***********************2 刪除學生記錄*************************\n");? ?? ?printf("***********************3 查找學生記錄*************************\n");? ?? ?printf("***********************4 修改學生記錄*************************\n");? ?? ?printf("***********************5 統(tǒng)計學生人數(shù) ************************\n");? ?? ?printf("***********************6 顯示學生記錄*************************\n");? ?? ?printf("***********************7 退出系統(tǒng) ****************************\n");? ?? ? }
2).增加學生記錄
void inputStudent(Link l){ ?? ? printf("請輸入學生學號:"); ?? ? scanf("%s",l->data.studentNo); ?? ? printf("請輸入學生的姓名:"); ?? ? scanf("%s",l->data.studentName); ?? ?printf("請輸入學生的成績:"); ?? ? scanf("%s",&(l->data.score)); ?? ? //每個新創(chuàng)建的節(jié)點的next域都初始化為NULL ?? ? l->next = NULL; ?? ? system("cls"); }
3).輸入學號接口·
void inputStudentNo(char s[],char no[]){ ?? ?printf("請輸入要%s的學生學號:",s); ?? ?scanf("%s",no); }
4).遍歷表中學生
//遍歷表中學生? void displayNode(Link head){ ?? ?if(head==NULL) ?? ?{ ?? ??? ?printf("學生為空\n"); ?? ??? ?return;? ?? ?} ?? ?else ?? ?{ ?? ??? ?Link p=head->next; ?? ??? ?while(p) ?? ??? ?{ ?? ??? ??? ?cout<<"姓名:"<<p->data.studentName<<" ? ?學號"<<p->data.studentNo<<" ?成績:"<<p->data.score<<endl;? ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ??? ? ?? ?} ? ?// 填寫代碼,根據(jù)傳入的鏈表head頭指針,掃描鏈表顯示所有節(jié)點的信息 ? ?system("pause"); ? ?system("cls"); }
5).增加學生記錄
/* 增加學生記錄 */ bool addNode(Link head){ ?? ? Link p,q; ? //p,q兩個節(jié)點一前一后 ?? ? Link node; ?//node指針指向新創(chuàng)建的節(jié)點 ?? ? node=(Link)malloc(sizeof(Node)); ?? ? inputStudent(node); ?? ? q = head; ?? ? p = head->next; ?//q指向head后面的第一個有效節(jié)點 ?? ? if(head->next==NULL) ?? ??? ? //鏈表為空時 ?? ??? ?head->next = node; ?? ? else { ?? ??? ? //循環(huán)訪問鏈表中的所有節(jié)點 ?? ??? ?while(p != NULL){ ?? ??? ??? ?if (node->data.studentNo < p->data.studentNo){ ?? ??? ??? ??? ?//如果node節(jié)點的學號比p節(jié)點的學號小,則插在p的前面,完成插入后,提前退出子程序 ?? ??? ??? ??? ?q->next = node; ?? ??? ??? ??? ?node->next = p; ?? ??? ??? ??? ?return true; ?? ??? ??? ?} ?? ??? ??? ?else{ ?? ??? ??? ??? ?//如果node節(jié)點的學號比p節(jié)點的學號大,繼續(xù)向后移動指針(依然保持pq一前一后) ?? ??? ??? ??? ?q = p; ?? ??? ??? ??? ?p = p->next; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//如果沒能提前退出循環(huán),則說明之前沒有插入,那么當前node節(jié)點的學號是最大值,此時插在鏈表的最后面 ?? ??? ?q->next = node; ?? ?} ?? ? return true; ?? ? system("pause"); ? ?system("cls"); }
6).刪除學生信息
//刪除學生信息 bool deleteNode(Link head){ ? ? // 按照給定的學號刪除學生記錄,如果刪除成功返回true,如果沒找到學號返回false ? ? //輸入要處理的學號 ? ? ?? ?char no[NO_LENGTH]; ?? ?inputStudentNo("刪除",no); ?? ??? ?Link p=head->next; ?? ?Link q=head; ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.studentNo,no)==0) ?? ??? ?{ ?? ??? ??? ?q->next=p->next; ?? ??? ??? ?free(p); ?? ??? ??? ?system("pause"); ? ?system("cls"); ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?q=p; ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ?} system("pause"); ? ?system("cls"); ?? ?return false; }
7).查找學生信息
//查找學生信息? bool queryNode(Link head){ ? ? // 按照給定的學號查詢學生記錄,如果查找成功返回true,如果沒找到學號返回false ? ? //輸入要處理的學號 ?? ?char no[NO_LENGTH]; ?? ?inputStudentNo("查找",no); ?? ??? ?Link p=head->next; ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.studentNo,no)==0) ?? ??? ?{ ?? ??? ??? ? ? ??? ??? ??? ?system("cls"); ? ??? ??? ??? ?cout<<"姓名:"<<p->data.studentName<<" ? ?學號"<<p->data.studentNo<<" ?成績:"<<p->data.score<<endl;? ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ?} ? ?system("cls"); ?? ?return false; }
8).修改學生信息
//修改學生信息? bool modifyNode(Link head){ ? ? // 按照給定的學號找到學生記錄節(jié)點,如果修改成功返回true,如果沒找到學號返回false ?? ? ? ? //輸入要處理的學號 ?? ?char no[NO_LENGTH]; ?? ?inputStudentNo("修改",no); ?? ?Link p=head->next; ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.studentNo,no)==0) ?? ??? ?{ ?? ??? ??? ?cout<<"請輸入修改后的姓名"<<endl;? ?? ??? ??? ?cin>>p->data.studentName; ?? ??? ??? ?cout<<"請輸入修改后的學號"<<endl;? ?? ??? ??? ?cin>>p->data.studentNo; ?? ??? ??? ?cout<<"請輸入修改后的成績"<<endl;? ?? ??? ??? ?cin>>p->data.score; ?? ??? ??? ?system("cls"); ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ?} ?? ?system("cls"); ?? ?return false; }
9).統(tǒng)計學生人數(shù)
//統(tǒng)計學生人數(shù) int countNode(Link head){ ?? ?//統(tǒng)計學生人數(shù),掃描鏈表統(tǒng)計節(jié)點個數(shù),返回節(jié)點數(shù) ?? ?Link p; ?? ?int count = 0; ?? ?p = head->next; ?? ?while(p) ?? ?{ ?? ??? ?p=p->next; ?? ??? ?count++; ?? ?} ?? ?//填充代碼 ?? ?system("cls"); ?? ?return count; }
10).清空鏈表
//清空鏈表? void clearLink(Link head){ ?? ?Link q,p; ?? ?p=head->next; ?? ?q=head; ?? ?while(p) ?? ?{ ?? ??? ?q->next=p->next; ?? ??? ?free(p); ?? ??? ?p=q->next; ?? ?} ? ? ? ? //遍歷鏈表,用free語句刪除鏈表中用malloc建立起的所有的節(jié)點 }
5.main函數(shù)
int main() { ?? ?int select; ? ? ?? ?int count; ?? ?Link head; ?// 定義鏈表 ?? ?//建立head頭結(jié)點,在這個程序中head指向頭結(jié)點,頭結(jié)點data部分沒有內(nèi)容,其后續(xù)節(jié)點才有真正的數(shù)據(jù) ?? ?head = (Link)malloc(sizeof(Node)); ?? ?head->next = NULL; ?? ?while(1) ?? ?{ ?? ??? ?myMenu(); ?? ??? ?printf("\n請輸入你的選擇(0-7):"); ?//顯示提示信息 ?? ??? ?scanf("%d",&select); ?? ??? ?switch(select) ?? ??? ?{ ?? ??? ?case 1: ?? ??? ??? ?//增加學生記錄 ?? ??? ??? ?if(addNode(head)) ?? ??? ??? ??? ?printf("成功插入一個學生記錄。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?//刪除學生記錄 ?? ??? ??? ?if(deleteNode(head)) ?? ??? ??? ??? ?printf("成功刪除一個學生記錄。\n\n"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf("沒有找到要刪除的學生節(jié)點。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?//查詢學生記錄 ?? ??? ??? ?if(queryNode(head)) ?? ??? ??? ??? ?printf("成功找到學生記錄。\n\n"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf("沒有找到要查詢的學生節(jié)點。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 4: ?? ??? ??? ?//修改學生記錄 ?? ??? ??? ?if(modifyNode(head)) ?? ??? ??? ??? ?printf("成功修改一個學生記錄。\n\n"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf("沒有找到要修改的學生節(jié)點。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?//統(tǒng)計學生人數(shù) ?? ??? ??? ?count = countNode(head); ?? ??? ??? ?printf("學生人數(shù)為:%d\n\n",count); ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?//顯示學生記錄 ?? ??? ??? ?displayNode(head); ?? ??? ??? ?break; ?? ??? ?case 7: ?? ??? ??? ?//退出前清除鏈表中的所有結(jié)點 ? ? ? ? ? ? clearLink(head); ?? ??? ??? ?return 0; ?? ??? ?default: ?? ??? ??? ?printf("輸入不正確,應該輸入0-7之間的數(shù)。\n\n"); ?? ??? ??? ?system("cls");? ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?return 0; }
6.總源碼(可直接復制運行)
#include <stdio.h> #include<string.h> #include<malloc.h> #include<stdlib.h> #include<stdbool.h> #include<iostream> using namespace std; #define NO_LENGTH ?20 #define NAME_LENGTH 11 /* 定義學生結(jié)構(gòu)體的數(shù)據(jù)結(jié)構(gòu) */ typedef struct Student{ ?? ?char studentNo[NO_LENGTH]; ?? ?char studentName[NAME_LENGTH]; ?? ?int score; }st; /* 定義每條記錄或節(jié)點的數(shù)據(jù)結(jié)構(gòu) */ typedef struct node { ?? ?struct Student data; //數(shù)據(jù)域 ?? ?struct node *next; //指針域 }Node,*Link; ?//Node為node類型的別名,Link為node類型的指針別名 //定義提示菜單 void myMenu(){ ?? ?printf("*****************************菜單*****************************\n");? ?? ?printf("***********************1 增加學生記錄*************************\n");? ?? ?printf("***********************2 刪除學生記錄*************************\n");? ?? ?printf("***********************3 查找學生記錄*************************\n");? ?? ?printf("***********************4 修改學生記錄*************************\n");? ?? ?printf("***********************5 統(tǒng)計學生人數(shù) ************************\n");? ?? ?printf("***********************6 顯示學生記錄*************************\n");? ?? ?printf("***********************7 退出系統(tǒng) ****************************\n");? ?? ? } void inputStudent(Link l){ ?? ? printf("請輸入學生學號:"); ?? ? scanf("%s",l->data.studentNo); ?? ? printf("請輸入學生的姓名:"); ?? ? scanf("%s",l->data.studentName); ?? ?printf("請輸入學生的成績:"); ?? ? scanf("%d",&(l->data.score)); ?? ? //每個新創(chuàng)建的節(jié)點的next域都初始化為NULL ?? ? l->next = NULL; ?? ? system("cls"); } void inputStudentNo(char s[],char no[]){ ?? ?printf("請輸入要%s的學生學號:",s); ?? ?scanf("%s",no); } //遍歷表中學生? void displayNode(Link head){ ?? ?if(head==NULL) ?? ?{ ?? ??? ?printf("學生為空\n"); ?? ??? ?return;? ?? ?} ?? ?else ?? ?{ ?? ??? ?Link p=head->next; ?? ??? ?while(p) ?? ??? ?{ ?? ??? ??? ?cout<<"姓名:"<<p->data.studentName<<" ? ?學號"<<p->data.studentNo<<" ?成績:"<<p->data.score<<endl;? ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ??? ? ?? ?} ? ?// 填寫代碼,根據(jù)傳入的鏈表head頭指針,掃描鏈表顯示所有節(jié)點的信息 ? ?system("pause"); ? ?system("cls"); } /* 增加學生記錄 */ bool addNode(Link head){ ?? ? Link p,q; ? //p,q兩個節(jié)點一前一后 ?? ? Link node; ?//node指針指向新創(chuàng)建的節(jié)點 ?? ? node=(Link)malloc(sizeof(Node)); ?? ? inputStudent(node); ?? ? q = head; ?? ? p = head->next; ?//q指向head后面的第一個有效節(jié)點 ?? ? if(head->next==NULL) ?? ??? ? //鏈表為空時 ?? ??? ?head->next = node; ?? ? else { ?? ??? ? //循環(huán)訪問鏈表中的所有節(jié)點 ?? ??? ?while(p != NULL){ ?? ??? ??? ?if (node->data.studentNo < p->data.studentNo){ ?? ??? ??? ??? ?//如果node節(jié)點的學號比p節(jié)點的學號小,則插在p的前面,完成插入后,提前退出子程序 ?? ??? ??? ??? ?q->next = node; ?? ??? ??? ??? ?node->next = p; ?? ??? ??? ??? ?return true; ?? ??? ??? ?} ?? ??? ??? ?else{ ?? ??? ??? ??? ?//如果node節(jié)點的學號比p節(jié)點的學號大,繼續(xù)向后移動指針(依然保持pq一前一后) ?? ??? ??? ??? ?q = p; ?? ??? ??? ??? ?p = p->next; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//如果沒能提前退出循環(huán),則說明之前沒有插入,那么當前node節(jié)點的學號是最大值,此時插在鏈表的最后面 ?? ??? ?q->next = node; ?? ?} ?? ? return true; ?? ? system("pause"); ? ?system("cls"); } bool deleteNode(Link head){ ? ? // 按照給定的學號刪除學生記錄,如果刪除成功返回true,如果沒找到學號返回false ? ? //輸入要處理的學號 ? ? ?? ?char no[NO_LENGTH]; ?? ?inputStudentNo("刪除",no); ?? ??? ?Link p=head->next; ?? ?Link q=head; ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.studentNo,no)==0) ?? ??? ?{ ?? ??? ??? ?q->next=p->next; ?? ??? ??? ?free(p); ?? ??? ??? ?system("pause"); ? ?system("cls"); ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?q=p; ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ?} system("pause"); ? ?system("cls"); ?? ?return false; } //查找學生信息? bool queryNode(Link head){ ? ? // 按照給定的學號查詢學生記錄,如果查找成功返回true,如果沒找到學號返回false ? ? //輸入要處理的學號 ?? ?char no[NO_LENGTH]; ?? ?inputStudentNo("查找",no); ?? ??? ?Link p=head->next; ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.studentNo,no)==0) ?? ??? ?{ ?? ??? ??? ? ? ??? ??? ??? ?system("cls"); ? ??? ??? ??? ?cout<<"姓名:"<<p->data.studentName<<" ? ?學號"<<p->data.studentNo<<" ?成績:"<<p->data.score<<endl;? ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ?} ? ?system("cls"); ?? ?return false; } //修改學生信息? bool modifyNode(Link head){ ? ? // 按照給定的學號找到學生記錄節(jié)點,如果修改成功返回true,如果沒找到學號返回false ?? ? ? ? //輸入要處理的學號 ?? ?char no[NO_LENGTH]; ?? ?inputStudentNo("修改",no); ?? ?Link p=head->next; ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.studentNo,no)==0) ?? ??? ?{ ?? ??? ??? ?cout<<"請輸入修改后的姓名"<<endl;? ?? ??? ??? ?cin>>p->data.studentName; ?? ??? ??? ?cout<<"請輸入修改后的學號"<<endl;? ?? ??? ??? ?cin>>p->data.studentNo; ?? ??? ??? ?cout<<"請輸入修改后的成績"<<endl;? ?? ??? ??? ?cin>>p->data.score; ?? ??? ??? ?system("cls"); ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?p=p->next; ?? ??? ?} ?? ?} ?? ?system("cls"); ?? ?return false; } //統(tǒng)計學生人數(shù) int countNode(Link head){ ?? ?//統(tǒng)計學生人數(shù),掃描鏈表統(tǒng)計節(jié)點個數(shù),返回節(jié)點數(shù) ?? ?Link p; ?? ?int count = 0; ?? ?p = head->next; ?? ?while(p) ?? ?{ ?? ??? ?p=p->next; ?? ??? ?count++; ?? ?} ?? ?//填充代碼 ?? ?system("cls"); ?? ?return count; } //清空鏈表? void clearLink(Link head){ ?? ?Link q,p; ?? ?p=head->next; ?? ?q=head; ?? ?while(p) ?? ?{ ?? ??? ?q->next=p->next; ?? ??? ?free(p); ?? ??? ?p=q->next; ?? ?} ? ? ? ? //遍歷鏈表,用free語句刪除鏈表中用malloc建立起的所有的節(jié)點 } int main() { ?? ?int select; ? ? ?? ?int count; ?? ?Link head; ?// 定義鏈表 ?? ?//建立head頭結(jié)點,在這個程序中head指向頭結(jié)點,頭結(jié)點data部分沒有內(nèi)容,其后續(xù)節(jié)點才有真正的數(shù)據(jù) ?? ?head = (Link)malloc(sizeof(Node)); ?? ?head->next = NULL; ?? ?while(1) ?? ?{ ?? ??? ?myMenu(); ?? ??? ?printf("\n請輸入你的選擇(0-7):"); ?//顯示提示信息 ?? ??? ?scanf("%d",&select); ?? ??? ?switch(select) ?? ??? ?{ ?? ??? ?case 1: ?? ??? ??? ?//增加學生記錄 ?? ??? ??? ?if(addNode(head)) ?? ??? ??? ??? ?printf("成功插入一個學生記錄。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?//刪除學生記錄 ?? ??? ??? ?if(deleteNode(head)) ?? ??? ??? ??? ?printf("成功刪除一個學生記錄。\n\n"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf("沒有找到要刪除的學生節(jié)點。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?//查詢學生記錄 ?? ??? ??? ?if(queryNode(head)) ?? ??? ??? ??? ?printf("成功找到學生記錄。\n\n"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf("沒有找到要查詢的學生節(jié)點。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 4: ?? ??? ??? ?//修改學生記錄 ?? ??? ??? ?if(modifyNode(head)) ?? ??? ??? ??? ?printf("成功修改一個學生記錄。\n\n"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf("沒有找到要修改的學生節(jié)點。\n\n"); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?//統(tǒng)計學生人數(shù) ?? ??? ??? ?count = countNode(head); ?? ??? ??? ?printf("學生人數(shù)為:%d\n\n",count); ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?//顯示學生記錄 ?? ??? ??? ?displayNode(head); ?? ??? ??? ?break; ?? ??? ?case 7: ?? ??? ??? ?//退出前清除鏈表中的所有結(jié)點 ? ? ? ? ? ? clearLink(head); ?? ??? ??? ?return 0; ?? ??? ?default: ?? ??? ??? ?printf("輸入不正確,應該輸入0-7之間的數(shù)。\n\n"); ?? ??? ??? ?system("cls");? ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?return 0; }
7.測試結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實現(xiàn)教職工管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細介紹了C++實現(xiàn)教職工管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03