首页 > 代码库 > 学生信息管理系统(C语言版本)

学生信息管理系统(C语言版本)

这是我个人写的一个学生管理系统,这是我仅仅用来练手的代码,要知道链表可是你在面试过程中最大机率会考到的,我是陆续从单向链表入门,然后采用双向链表写的代码!如有BUG,请指正,让我们
共同进步!
1
#include<stdio.h> 2 #include<malloc.h> 3 #include<string.h> 4 struct student 5 { 6 int id; 7 char name[100]; 8 int age; 9 int sex; 10 int class_num; 11 struct student *next; 12 struct student *pr; 13 }; 14 typedef struct student link; 15 link *head; 16 int insert() 17 { 18 link *newnode; 19 newnode = (link *)malloc(sizeof(struct student)); 20 link *temp_head = head; 21 printf("请输入学生ID:\n"); 22 scanf("%d",&newnode->id); 23 printf("请输入学生姓名:\n"); 24 scanf("%s",newnode->name); 25 printf("请输入学生年龄:\n"); 26 scanf("%d",&newnode->age); 27 printf("请输入学生性别0(代表男 1代表女):\n"); 28 scanf("%d",&newnode->sex); 29 if((newnode->sex == 0|| newnode->sex == 1) == 0) 30 { 31 printf("input is fail!\n"); 32 scanf("%d",&newnode->sex); 33 } 34 printf("请输入学生的班级号码:\n"); 35 scanf("%d",&newnode->class_num); 36 newnode->next = NULL; 37 newnode->pr = NULL; 38 if(temp_head->next == NULL) 39 { 40 temp_head->next = newnode; 41 newnode->pr = temp_head; 42 newnode->next = NULL; 43 return 0; 44 } 45 int flag = 0; 46 while(temp_head->next != NULL) 47 { 48 if(newnode->id < temp_head->next->id) 49 { 50 newnode->next = temp_head->next; 51 newnode->pr = temp_head; 52 temp_head->next = newnode; 53 newnode->next->pr = newnode; 54 flag = 1; 55 break; 56 } 57 temp_head = temp_head->next; 58 } 59 if(flag == 0) 60 { 61 temp_head->next = newnode; 62 newnode->next = NULL; 63 newnode->pr = temp_head; 64 } 65 return 0; 66 } 67 int input() 68 { 69 char num[100]; 70 scanf("%s",num); 71 if(strcmp(num,"insert") == 0) 72 { 73 return 1; 74 } 75 else if(strcmp(num,"delete") == 0) 76 { 77 return 2; 78 } 79 else if(strcmp(num,"display") == 0) 80 { 81 return 3; 82 } 83 else if(strcmp(num,"exit") == 0) 84 { 85 return 4; 86 } 87 else 88 { 89 return 0; 90 } 91 } 92 int display(int id) 93 { 94 link *temp_head = head; 95 int flag = 0; 96 if(temp_head->next == NULL) 97 { 98 printf("没有输入学生信息:\n"); 99 return 0;100 }101 while(temp_head != NULL)102 {103 if(temp_head->id == id)104 {105 printf("id = %d\n name = %s\n age = %d\n sex = %d\n class_num = %d\n",temp_head->id,temp_head->name,temp_head->age,temp_head->sex,temp_head->class_num);106 flag = 1;107 break;108 }109 temp_head = temp_head->next;110 }111 if(flag == 0)112 {113 printf("查无此人!\n"); 114 }115 return 0;116 }117 int delete(int id)118 {119 link *temp_head = head;120 int flag;121 if(temp_head->next == NULL)122 {123 printf("学生内容为空,无法删除\n");124 return 0;125 }126 while(temp_head != NULL)127 {128 if(temp_head->id == id)129 {130 if(temp_head->next == NULL)131 {132 temp_head->pr->next = NULL;133 flag = 1;134 break;135 }136 else137 {138 temp_head->pr->next = temp_head->next;139 temp_head->next->pr = temp_head->pr;140 flag = 1;141 break;142 } 143 }144 temp_head = temp_head->next;145 146 }147 if(flag == 1)148 {149 printf("删除成功!\n");150 }151 else152 {153 printf("删除失败!\n");154 }155 return 0;156 }157 int main()158 {159 int pid;160 int id;161 int class_num;162 head = (link *)malloc(sizeof(struct student));163 head->next = NULL;164 head->pr = NULL;165 printf("/****************************\n");166 printf("\t欢迎使用学生管理系统\n");167 printf("\t输入insert添加学生信息\n");168 printf("\t输入delete删除学生信息\n");169 printf("\t输入display查看学生信息\n");170 printf("\t输入exit退出程序\n");171 printf("*************************/\n");172 printf("请输入命令!\n");173 pid = input();174 while(pid != 4)175 {176 switch(pid)177 {178 case 1 :179 insert();180 break;181 case 2 :182 printf("请输入要删除的学生ID:\n");183 scanf("%d",&id);184 delete(id);185 break;186 case 3 :187 printf("请输入要显示的学生ID:\n");188 scanf("%d",&id);189 display(id);190 break;191 case 0 :192 printf("输入的命令有误,请重新输入:\n");193 break;194 case 4 :195 break;196 }197 printf("请输入命令!\n");198 pid = input();199 }200 printf("该程序结束!\n");201 return 0;202 }