c语言之一个简单的《学生教师管理系统》小结记录(二) 本篇博文用来记录学生头/教师文件建立以及结构体链表创建及链表相关操作
首先是头文件的建立
头文件包含学生结构体以及链表结构
1、学生结构体建立
1 /****定义学生信息******/ 2 3 typedef struct studentInfo 4 { 5 int ID; 6 char name[16]; 7 char password[16]; 8 int age; 9 int classes; 10 char sex; 11 float math; 12 float chinese; 13 float clanguage; 14 }StuInfo;
2、链表结构建立
1 /*定义链表结构*/ 2 typedef struct studentNode 3 { 4 struct studentInfo data; 5 struct studentNode *pNext; 6 }Node;
3、函数声明
1 /*创建链表结构并且申请空间*/ 2 Node* makeNode(); 3 4 /*学生信息初始化*/ 5 StuInfo initData(); 6 7 /*头插法*/ 8 void headInsert(Node *pHead); 9 10 /*循环处理,将节点的数据域写入文件*/ 11 void writeToFile(Node *pHead); 12 13 /*遍历链表*/ 14 void showList(Node *pHead); 15 16 /*从文件中读取链表信息*/ 17 Node* readFromFile(Node *pHead); 18 19 /*查找学生*/ 20 Node* findnode(Node *pHead, const int ID); 21 22 /*录入查重*/ 23 Node* findstu(Node *pHead, const int ID); 24 25 /*按班级遍历链表*/ 26 void showClassList(Node *pHead, int classes); 27 28 /*按总成绩显示排序*/ 29 void showClassSortList(Node *pHead, int classes); 30 31 /*修改学生信息初始化*/ 32 StuInfo modinitData();
4、整个头文件
1 #ifndef __STUDENT_H_ 2 #define __STUDENT_H_ 3 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <malloc.h> 8 9 #define STU_LEN sizeof(StuInfo) 10 #define NODE_LEN sizeof(Node) 11 12 /****定义学生信息******/ 13 14 typedef struct studentInfo 15 { 16 int ID; 17 char name[16]; 18 char password[16]; 19 int age; 20 int classes; 21 char sex; 22 float math; 23 float chinese; 24 float clanguage; 25 }StuInfo; 26 27 28 /*定义链表结构*/ 29 typedef struct studentNode 30 { 31 struct studentInfo data; 32 struct studentNode *pNext; 33 }Node; 34 35 /*创建链表结构并且申请空间*/ 36 Node* makeNode(); 37 38 /*学生信息初始化*/ 39 StuInfo initData(); 40 41 /*头插法*/ 42 void headInsert(Node *pHead); 43 44 /*循环处理,将节点的数据域写入文件*/ 45 void writeToFile(Node *pHead); 46 47 /*遍历链表*/ 48 void showList(Node *pHead); 49 50 /*从文件中读取链表信息*/ 51 Node* readFromFile(Node *pHead); 52 53 /*查找学生*/ 54 Node* findnode(Node *pHead, const int ID); 55 56 /*录入查重*/ 57 Node* findstu(Node *pHead, const int ID); 58 59 /*按班级遍历链表*/ 60 void showClassList(Node *pHead, int classes); 61 62 /*按总成绩显示排序*/ 63 void showClassSortList(Node *pHead, int classes); 64 65 /*修改学生信息初始化*/ 66 StuInfo modinitData(); 67 68 #endif
5、函数实现
1 /*创建链表节点*/ 2 Node* makeNode() 3 { 4 Node *newnode = (Node *)malloc(NODE_LEN);//为新节点分配空间 5 if(NULL == newnode)//容错处理,如果分配失败再次申请空间 6 { 7 newnode = (Node *)malloc(NODE_LEN); 8 } 9 memset(&newnode->data, '