|
发表于 2019-3-16 01:09:55
|
显示全部楼层
本帖最后由 jackz007 于 2019-3-16 01:35 编辑
你的代码太乱,看起来很费劲,我又按照你的程序框架重写了全部代码供你参考。
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- typedef struct student
- {
- int num ;
- float score ;
- struct student * next ;
- } node ;
- int n ;
- //链表插入
- node * insert(node * pstu)
- {
- node * head , * p1 , * p2 ;
- int num ;
- float score ;
- bool f ;
- head = pstu ;
- for(;;) {
- printf("please input stuent number(0 to terminate): ") ;
- scanf("%d", & num) ;
- if(num == 0) {
- break ;
- } else {
- f = true ;
- p1 = p2 = head ;
- while(p2 != NULL) {
- if(p2 -> num == num) {
- f = false ;
- break ;
- } else {
- p1 = p2 ;
- p2 = p1 -> next ;
- }
- }
- if(f) {
- printf("please input stuent score: ") ;
- scanf("%f", & score) ;
- p2 = (node *) malloc(sizeof(node)) ;
- if(p2 != NULL) {
- p2 -> num = num ;
- p2 -> score = score ;
- p2 -> next = NULL ;
- if(! n) head = p2 ;
- else p1 -> next = p2 ;
- n ++ ;
- } else {
- fprintf(stderr , "error : allocate memory operation failure !\n") ;
- }
- } else {
- printf("抱歉,学号为 %d 的学生已经存在!\n" , num) ;
- }
- }
- }
- return head ;
- }
- //链表节点删除函数
- node * del(node * pstu)
- {
- node * p1 , * p2 , * head ;
- int num , m ;
- bool f ;
- head = pstu ;
- if(n > 0) {
- while(n > 0) {
- printf("please input stuent number(0 to terminate): ") ;
- scanf("%d", & num) ;
- if(num != 0) {
- f = false ;
- p2 = head ;
- m = 0 ;
- while(p2 != NULL) {
- if(p2 -> num == num) {
- if(! m) head = p2 -> next ;
- else p1 -> next = p2 -> next ;
- f = true ;
- break ;
- } else {
- p1 = p2 ;
- p2 = p1 -> next ;
- }
- m ++ ;
- }
- if(f) {
- free(p2) ;
- printf("成功删除学号为 %d 的学生\n" , num) ;
- n -- ;
- } else {
- printf("抱歉,没有找到学号为 %d 的学生\n" , num) ;
- }
- } else {
- break ;
- }
- }
- } else {
- printf("链表为空!!!\n\n") ;
- }
- return head ;
- }
- //打印函数
- void print(node * pstu)
- {
- node * p ;
- if(n > 0) {
- p = pstu ;
- printf("There are %d records!!!\n", n) ;
- while (p != NULL) {
- printf("student number = %d\n" , p -> num) ;
- printf("student score = %5.2f\n\n" , p -> score) ;
- p = p -> next ;
- }
- } else {
- printf("链表为空!!!\n\n") ;
- }
- }
- int main(void)
- {
- node * stu ;
- char c ;
- stu = insert(NULL) ;
- for(;;) {
- printf("\n") ;
- printf("**********************************************\n") ;
- printf("* 学生成绩信息管理系统 *\n") ;
- printf("**********************************************\n") ;
- printf(" 1. 添加学生成绩信息.\n") ;
- printf(" 2. 删除学生成绩信息.\n") ;
- printf(" 3. 显示学生成绩信息.\n") ;
- printf(" 0. 退出学生成绩信息系统.\n") ;
- printf("----------------------------------------------\n") ;
- printf(" 请选择(0 - 4):[1] ") ;
- c = getch() ;
- printf("\n\n") ;
- if((c > 0x2f && c < 0x34) || c == 0x03 || c == 0x0d) {
- if(c == 0x03 || c == 0x30) {
- break ;
- } else {
- switch(c) {
- case 0x0d:
- case 0x31:
- stu = insert(stu) ;
- break ;
- case 0x32:
- stu = del(stu) ;
- break ;
- case 0x33:
- print(stu) ;
- break ;
- }
- }
- }
- }
- system("pause") ;
- }
复制代码 |
|