实验要求
利用单向循环链表实现一个班级学生信息管理(数据录入、插入、删除、排序、查找等,并能够实现将数据存储到文件中)
代码实现
typedef struct InfoList {
int age;
char name[20];
char major[20];
char id[15];
struct InfoList* next;
} InfoList, * inode;
InfoList* init_node() {
inode head = malloc(sizeof(InfoList));
head->next = head;
strcpy(head->name, " ");
strcpy(head->major, " ");
strcpy(head->id, " ");
head->age = 0;
return head;
}
void creat_infoList(inode p) {
inode s = p, q;
int age, n, i;
char name[20];
char major[20];
char id[15];
printf("请输入要录入的人数:");
scanf("%d", &n);
getchar(); // 清空换行符
for (i = 0; i < n; i++) {
q = (inode)malloc(sizeof(InfoList));
printf("\nStudent %d \n", i + 1);
printf("请输入姓名:");
fgets(name, 20, stdin);
name[strcspn(name, "\n")] = 0; // 去除换行符
strcpy(q->name, name);
printf("请输入年龄:");
scanf("%d", &age);
getchar(); // 清空换行符
q->age = age;
printf("请输入ID:");
fgets(id, 15, stdin);
id[strcspn(id, "\n")] = 0; // 去除换行符
strcpy(q->id, id);
printf("请输入专业:");
fgets(major, 20, stdin);
major[strcspn(major, "\n")] = 0; // 去除换行符
strcpy(q->major, major);
s->next = q;
q->next = p;
s = q;
}
}
void insert_inodes(inode head) {
inode q = (inode)malloc(sizeof(InfoList));;
inode s = head->next;
while (s->next != head) {
s = s->next;
}
int age;
char name[20];
char major[20];
char id[15];
int n, i;
printf("请输入要增加的人数:");
scanf("%d", &n);
getchar(); // 清空换行符
for (i = 0; i < n; i++) {
q = (inode)malloc(sizeof(InfoList));
printf("\nStudent %d \n", i + 1);
printf("请输入姓名: ");
fgets(name, 20, stdin);
name[strcspn(name, "\n")] = 0; // 去除换行符
strcpy(q->name, name);
printf("请输入年龄: ");
scanf("%d", &age);
getchar(); // 清空换行符
q->age = age;
printf("请输入ID: ");
fgets(id, 15, stdin);
id[strcspn(id, "\n")] = 0; // 去除换行符
strcpy(q->id, id);
printf("请输入专业: ");
fgets(major, 20, stdin);
major[strcspn(major, "\n")] = 0; // 去除换行符
strcpy(q->major, major);
s->next = q;
q->next = head;
s = q;
}
}
void delete_inodes(inode head) {
inode q = head;
inode s = head->next;
char id[15];
int f = 0;
printf("请输入要删除学生的ID:");
getchar(); //如果没有会无法输入id
fgets(id, 15, stdin);
id[strcspn(id, "\n")] = 0; // 去除换行符
while (s != head) {
if (strcmp(s->id,id)==0){
q->next = s->next;
free(s);
printf("该学生信息已删除\n");
f = 1;
}
q = s;
s = s->next;
if (f == 1) break;
}
if (f == 0) {
printf("没有找到该学生的信息!\n");
}
}
void change_inode(inode head) {
inode s = head->next;
int f=0;
int age;
char id[15];
char name[20];
char major[20];
printf("请输入要修改学生的ID: ");
scanf("%s", id);
while (s != head) {
if (strcmp(s->id, id) == 0) {
f = 1;
printf("请输入姓名: ");
scanf("%s", name);
strcpy(s->name, name);
printf("请输入年龄: ");
scanf("%d", &age);
s->age = age;
printf("请输入专业: ");
scanf("%s", major);
strcpy(s->major, major);
printf("修改成功!\n");
break;
}
s = s->next;
}
if (f == 0) {
printf("没有找到该学生信息!\n");
}
}
void search_inode(inode head) {
inode s = head->next;
char id[15];
int f = 0;
printf("请输入要查找学生的ID: ");
scanf("%s", id);
while (s != head) {
if (strcmp(s->id, id) == 0) {
f = 1;
printf("该学生的信息如下: \n");
printf("name: %s\n",s->name);
printf("age: %d\n", s->age);
printf("ID: %s\n", s->id);
printf("major: %s\n", s->major);
break;
}
s = s->next;
}
if (f == 0) {
printf("没有找到该学生信息!\n");
}
}
void sort_inodes(inode head) {
printf("********默认按照ID从小到大排序********\n");
inode s, start, end, pre, post;
//start,end为排好序的新链表始末,s为要插入的节点,在pre与post之间插入节点
start = (inode)malloc(sizeof(InfoList));
start->next = head;
end = head;
s = end->next;
while (s != head) {
pre = start;
post = start->next;
while (post != s && strcmp(post->id, s->id) < 0) {
post = post->next;
pre = pre->next;
}
if (post == s) end = s; //有序节点都小于待插入节点
else {
end->next = s->next;
s->next = post;
pre->next = s;
}
s = end->next;
}
head = start->next;
free(start);
printf("排序已完成!\n");
}
void printInfo(inode head) {
inode p;
int i = 1;
for (p = head->next; p!= head; p = p->next) {
printf("\nStudent %d:\n", i);
printf("+-------------------------------------------------------------------+\n");
printf("| name | age | ID | major |\n");
printf("+-------------------+--------------+-----------------+--------------+\n");
printf("| %-14s| %-8d | %-12s | %-9s |\n", p->name, p->age, p->id, p->major);
printf("+-------------------+--------------+-----------------+--------------+\n");
i++;
}
}
void saveInfo(inode head) {
FILE* f = fopen("InfoList.csv", "w");
if (f == NULL) {
printf("Error openning file");
return;
}
inode s = head->next;
while (s != head) {
fprintf(f, "\"%s\",\"%d\",\"%s\",\"%s\"\n", s->name, s->age, s->id, s->major);
s = s->next;
}
fclose(f);
printf("信息已存入InfoList.csv中\n");
}
int menu() {
printf("\n**********欢迎使用学生信息管理系统**************\n");
printf("********** 1.录入学生信息 **************\n");
printf("********** 2.增加学生信息 **************\n");
printf("********** 3.删除学生信息 **************\n");
printf("********** 4.修改学生信息 **************\n");
printf("********** 5.查找学生信息 **************\n");
printf("********** 6.排序 **************\n");
printf("********** 7.显示学生信息 **************\n");
printf("********** 0.退出管理系统 **************\n\n");
printf("********** 请输入选项: ");
int op;
scanf("%d", &op);
printf("\n");
return op;
}
void test() {
int op;
inode s = init_node();
do {
op = menu();
switch (op) {
case 0:
saveInfo(s);
break;
case 1:
creat_infoList(s);
break;
case 2:
insert_inodes(s);
break;
case 3:
delete_inodes(s);
break;
case 4:
change_inode(s);
break;
case 5:
search_inode(s);
break;
case 6:
sort_inodes(s);
break;
case 7:
printInfo(s);
break;
default:
printf("请重新输入选项\n");
break;
}
} while (op != 0);
}
int main() {
test();
printf("\n***************欢迎再次使用学生信息管理系统!***************\n");
return 0;
}
结果展示
参考资料
http://t.csdnimg.cn/JR5wN ---单向循环链表
http://t.csdnimg.cn/2GWeJ---链表插入排序