/* ** File: Main.c* Descript: 单链表操作* Author: Red_angelX */ # include <stdio.h> # define MAXNUM 100 /* ** 学生结构体链表* */ typedef struct student{ int number ; char name[ 10 ]; int score; // 新增链表指针 struct student * next ;}student; /* ** 创建链表* */ student * create(){ int n , i; student * head ,* preview ,* current ; /* head表头 preview前一个 current当前 */ if (( head = (student * )(malloc( sizeof (student)))) == NULL ) { printf ( " 无法分配内存 " ); exit ( 0 ); } head -> next = NULL ; /* 表头置空 初始化 */ preview = head; /* preview 指向表头 */ printf ( " 请输入学生数: " ); scanf( " %d " ,& n); for (i = 0 ;i < n;i ++ ) { if (( current = (student * )(malloc( sizeof (student)))) == NULL ) { printf ( " 无法分配内存 " ); exit ( 0 ); } preview -> next = current ; /* 把前一个节点指向当前节点,连接所有链表 */ printf ( " 请输入学生%d的数据: " , i + 1 ); printf ( " 学号: " ); scanf( " %d " ,& current -> number ); printf ( " 姓名: " ); scanf( " %s " ,& current -> name); printf ( " 成绩: " ); scanf( " %d " ,& current -> score); current -> next = NULL ; preview = current ; } return (head);} /* ** 查找节点* */ void find(student * stu){ int x; student * head; printf ( " 请输入要查找学生的学号: " ); scanf( " %d " ,& x); head = stu; while (head && head -> number != x) { head = head -> next ; /* 指向下条记录 */ } if (head) { printf ( " 学号 姓名 成绩 " ); printf ( " %6d " , head -> number ); printf ( " %s " , head -> name); printf ( " %6d " , head -> score); } else { printf ( " 没找到! " ); }} /* **插入节点*就当往最后插入了* */ void insert(student * stu){ int i , n; student * head ,* current ; printf ( " 请插入学生数: " ); scanf( " %d " ,& n); head = stu; // 把head移动到最后 while (head -> next ) head = head -> next ; for (i = 0 ;i < n;i ++ ) { if (( current = (student * )(malloc( sizeof (student)))) == NULL ) { printf ( " 无法分配内存 " ); exit ( 0 ); } head -> next = current ; printf ( " 请输入学生%d的数据: " , i + 1 ); printf ( " 学号: " ); scanf( " %d " ,& current -> number ); printf ( " 姓名: " ); scanf( " %s " ,& current -> name); printf ( " 成绩: " ); scanf( " %d " ,& current -> score); current -> next = NULL ; head = current ; }} /* ** 删除节点* */ void delete(student * stu){ int x; student * head ,* temp; printf ( " 输入要删除成绩学生的学号: " ); scanf( " %d " ,& x); head = stu; while (head -> next && head -> next -> number != x) { head = head -> next ; /* 指向下条记录 */ } if (head -> next ) { temp = head -> next ; head -> next = head -> next -> next ; free(temp); printf ( " 已删除! " ); } else { printf ( " 无此学生成绩,无法删除! " ); }} /* ** 主函数,链表操作* * Author: Red_angelX */ void main(){ student * stu; int k; while ( 1 ) { // clrscr(); printf ( " ****************************** " ); printf ( " | 学生成绩管理 | " ); printf ( " |*******************************| " ); printf ( " | 1 登记成绩 | " ); printf ( " | 2 查询成绩 | " ); printf ( " | 3 插入成绩 | " ); printf ( " | 4 删除成绩 | " ); printf ( " | 0 退出程序 | " ); printf ( " |*******************************| " ); printf ( " | Code By Red_angelX | " ); printf ( " ******************************* " ); printf ( " " ); printf ( " 请输入选择的功能号: " ); scanf( " %d " ,& k); switch (k) { case 1 : stu = create(); break ; case 2 : find(stu); break ; case 3 : insert(stu); break ; case 4 : delete(stu); break ; case 0 : exit ( 0 ); default : printf ( " 选择错误! " ); } } }