学生管理系统——基于双向循环链表
基于雙向循環(huán)鏈表實現(xiàn)的學(xué)生管理系統(tǒng),包括初始化,插入,刪除,查抄,保存,自動按照姓名排序功能,退出并保存功能。
實現(xiàn)思想是將程序的各個部分劃分為三個層次。主函數(shù)為界面層,即客戶端層;其中后綴為Student的一般是某個功能的調(diào)度函數(shù),屬于邏輯層的內(nèi)容;在調(diào)度函數(shù)之下有相應(yīng)的被調(diào)度的函數(shù),也就是相應(yīng)功能的實現(xiàn)函數(shù),一般后綴名為Node,意思就是這個函數(shù)直接操作鏈表中的結(jié)點,可以簡單的劃分為實現(xiàn)層;
這樣分層實現(xiàn)呢有利于代碼維護和個功能之間對包含或者重疊功能的直接調(diào)用,從而提高代碼重用度,而降低代碼冗余,并且最低成的實現(xiàn)函數(shù)也可以用于別的項目中對雙向循環(huán)鏈表的操作。
本次學(xué)生管理系統(tǒng)的實現(xiàn)是先用一個初始化文件函數(shù)將一些學(xué)生信息先存入磁盤中,然后用初始化函數(shù)讀出到鏈表中進行各種操作,保存功能和退出功能可以將鏈表中操作了的內(nèi)容,也就是內(nèi)存中的內(nèi)容寫入磁盤中,其中對文件的操作采用二進制讀寫文件,讀寫對象為學(xué)生結(jié)構(gòu)體。
以下是代碼實現(xiàn):
#include<stdio.h> #include<stdlib.h> #include<string.h> #pragma warning (disable:4996) //定義學(xué)生信息結(jié)點 struct SNode{char name[50];char sex;int math;int chinese;int history; }; typedef struct SNode Student; //定義鏈表結(jié)點 struct Node{Student student;struct Node *left;struct Node *right; }; typedef struct Node Node; void init(void);//初始化一個學(xué)生信息文件 Node * createList();//創(chuàng)建雙向循環(huán)鏈表 void initializationList(Node *head);//初始化鏈表 int insertStudent(Node *head);//菜單中插入功能調(diào)度函數(shù) int insertList(Node *head, Student newStudent);//插入功能實現(xiàn)函數(shù),頭插法插入結(jié)點 void searchStudent(Node *head);//查找功能的調(diào)度函數(shù) Node *searchNode(Node *head, char *name);//查找功能的實現(xiàn)函數(shù) int deleteStudent(Node *head);//菜單中刪除功能的調(diào)度函數(shù) int deleteNode(Node *head, char *name);//刪除功能的實現(xiàn)函數(shù) int lenList(Node *head);//求雙向循環(huán)鏈表的長度 void sortList(Node *head, int len);//依據(jù)姓名字符串的排序函數(shù) int saveToFile(Node *head);//將存放在內(nèi)存中的學(xué)生數(shù)據(jù)寫回磁盤文件中 void printList(Node * head);//打印鏈表內(nèi)容int main(void) { // init();//若沒有學(xué)生信息文件,可打開此函數(shù),初始化完成后可關(guān)閉此函數(shù),//可在本函數(shù)內(nèi)修改初始學(xué)生信息printf("學(xué)生管理系統(tǒng)\n");printf("目前所有學(xué)生:\n");Node *head = createList();//創(chuàng)建雙向循環(huán)鏈表管理學(xué)生initializationList(head);//從文件中讀取初始學(xué)生信息初始化鏈表while (1){int len = lenList(head);sortList(head, len);//輸出之前按照名字排序system("CLS");//調(diào)用系統(tǒng)清屏函數(shù)printf("學(xué)生管理系統(tǒng)\n");printf("目前所有學(xué)生:\n");printList(head);//打印鏈表中的信息printf("1->添加\t2->刪除\t3->查找4->保存5->退出\n");int select = 0;scanf("%d",&select);switch (select){case 1:insertStudent(head);system("CLS");printf("學(xué)生管理系統(tǒng)\n");printf("目前所有學(xué)生:\n");printList(head);break;case 2:deleteStudent(head);system("CLS");printf("學(xué)生管理系統(tǒng)\n");printf("目前所有學(xué)生:\n");printList(head);break;case 3:searchStudent(head);printf("請按下任意鍵返回主菜單\n");getchar();break;case 4:saveToFile(head);printf("保存成功\n");system("CLS");printf("學(xué)生管理系統(tǒng)\n");printf("目前所有學(xué)生:\n");initializationList(head);printList(head);break;case 5:printf("程序即將退出\n");saveToFile(head);printf("保存成功\n");exit(1);break;}}return 0; } //創(chuàng)建初始化文件 void init(void) {FILE *fop = fopen("D:/Test/classTest/student.txt", "wb");Student students[5] = { { "zhao", 'm', 90, 100, 100 },{ "qian", 'w', 60, 60, 60 },{ "sun", 'm', 85, 85, 90 },{ "li", 'm', 60, 100, 100 },{ "zhou", 'w', 100, 100, 100 } };fwrite(students, sizeof(Student), 5, fop);fclose(fop); } //創(chuàng)建雙向循環(huán)鏈表 Node * createList() {Node *head = (Node *)malloc(sizeof(Node));if (head){head->left = head;head->right = head;return head;}elsereturn NULL; } //初始化鏈表 void initializationList(Node *head) {FILE *fip = fopen("D:/Test/classTest/student.txt", "rb");Student newStudent;while (fread(&newStudent, sizeof(Student), 1, fip) != 0){insertList(head, newStudent);} } //菜單中插入功能調(diào)度函數(shù) int insertStudent(Node *head) {Student newStudent;printf("請輸入學(xué)生信息:\n");printf("姓名:");scanf("%s",&newStudent.name);getchar();printf("性別:");newStudent.sex=getchar();printf("數(shù)學(xué):");scanf("%d",&newStudent.math);printf("語文:");scanf("%d",&newStudent.chinese);printf("歷史:");scanf("%d",&newStudent.history);insertList(head, newStudent);} //結(jié)點插入 //插入功能實現(xiàn)函數(shù),頭插法插入 int insertList(Node *head, Student data) {Node *newNode = (Node *)malloc(sizeof(Node));if (newNode){newNode->student = data;newNode->left = head;newNode->right = head->right;head->right = newNode;newNode->right->left = newNode;return 1;}elsereturn -1; } //菜單中刪除功能的調(diào)度函數(shù) int deleteStudent(Node *head) {printf("請輸入需要刪除的學(xué)生的姓名:");char name[50];scanf("%s",name);getchar();int flag=deleteNode(head, name);if (flag)printf("刪除成功\n");elseprintf("刪除失敗\n");return flag; } //刪除功能的實現(xiàn)函數(shù) int deleteNode(Node *head, char *name) {Node *searchStudent = searchNode(head, name);if (searchStudent){searchStudent->left->right = searchStudent->right;searchStudent->right->left = searchStudent->left;free(searchStudent);return 1;}return 0; } //查找功能的調(diào)度函數(shù) void searchStudent(Node *head) {printf("請輸入需要刪除的學(xué)生的姓名:");char name[50];scanf("%s", name);getchar();Node *result = searchNode(head, name);if (result){printf("姓名\t性別\t數(shù)學(xué)\t語文\t歷史\n");printf("%s\t%c\t%d\t%d\t%d\n", result->student.name, result->student.sex,result ->student.math, result->student.chinese, result->student.history);}elseprintf("沒有找到該學(xué)生\n"); } //查找功能的實現(xiàn)函數(shù) Node *searchNode(Node *head, char *name) {Node *left = head;Node *right = head;do{left = left->left;right = right->right;if (strcmp(left->student.name, name)==0)return left;if (strcmp(right->student.name, name)==0)return right;} while (left != right && left->left != right);return NULL; } //依據(jù)姓名字符串的排序函數(shù) void sortList(Node *head, int len) {Node *p, *q, *max, *temp;Student t;p = head->right;q = p->right;int i = 0, j = 0;for (i = 0; i < len - 1; i++){if (p == head)break;max = p;q = p;for (j = i; j < len; j++){if (q == head)break;if (strcmp(max->student.name, q->student.name)>0)max = q;q = q->right;}if (max != p){t = max->student;max->student = p->student;p->student = t;}p = p->right;} } //求雙向循環(huán)鏈表的長度 int lenList(Node *head) {Node *p = head;int len = 0;while (p->right != head){len++;p = p->right;}return len; } //打印鏈表內(nèi)容 void printList(Node * head) {Node *p = head->right;while (p != head){printf("%s\t%c\t%d\t%d\t%d\n", p->student.name, p->student.sex,p->student.math, p->student.chinese, p->student.history);p = p->right;} } //將存放在內(nèi)存中的學(xué)生數(shù)據(jù)寫回磁盤文件中 int saveToFile(Node *head) {FILE *fop = fopen("D:/Test/classTest/student.txt", "wb");Node *p = head->right;while (p != head){fwrite(&p->student,sizeof(Student), 1, fop);p = p->right;}return 1; }文章轉(zhuǎn)載自 開源中國社區(qū)[https://www.oschina.net]
超強干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的学生管理系统——基于双向循环链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python基础-------pytho
- 下一篇: Java平台模块化系统(JSR 376)