|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求教,这个程序在删除头结点的时候会出错 请求大佬支援,指出错误,十分感谢!代码格式有问题也希望大佬指出,会努力改进的。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXSIZE 100 //内存池最大容量
- //结构体定义
- typedef struct student *ptrL;
- struct student
- {
- char num[10];
- char name[10];
- float score[3];
- ptrL next;
- };
- //函数声明
- void print(ptrL p);
- ptrL addStu(ptrL p);
- ptrL findStu(ptrL p);
- void change(ptrL p);
- ptrL deleteStu(ptrL p);
- void display(ptrL p);
- void exitlist(ptrL p);
- //内存池
- ptrL pool = NULL;
- int count = 0;
- //输出学生信息
- void print(ptrL p)
- {
- if (p == NULL)
- return;
- printf("该学生信息为:\n学号:%s\n姓名:%s\n", p->num, p->name);
- for (int i = 0; i < 3; i++)
- {
- if (i == 0)
- printf("语文");
- else if (i == 1)
- printf("数学");
- else
- printf("英语");
- printf("成绩为:%.2f\n", p->score[i]);
- }
- return;
- }
- //插入函数
- ptrL addStu(ptrL p)
- {
- ptrL p1, s = p;
- if (pool != NULL)
- {
- p1 = pool;
- pool = pool->next;
- count--;
- }
- else
- {
- p1 = (ptrL)malloc(sizeof(struct student));
- if (p1 == NULL)
- {
- printf("申请失败!\n");
- return NULL;
- }
- }
- p1->next = NULL;
- printf("******请输入学生信息******\n请输入学生学号:");
- scanf("%s", p1->num);
- printf("请输入学生姓名:");
- scanf("%s", p1->name);
- for (int i = 0; i < 3; i++)
- {
- if (i == 0)
- printf("请输入学生语文成绩:");
- else if (i == 1)
- printf("请输入学生数学成绩:");
- else
- printf("请输入学生英语成绩:");
- scanf("%f", &p1->score[i]);
- }
- if (p == NULL)
- {
- p = p1;
- printf("创建第一个学生成功!\n");
- return p;
- }
- while (s->next != NULL)
- {
- s = s->next;
- }
- s->next = p1;
- printf("插入成功!\n");
- return p;
- }
- //查找函数(按学号查找)返回学生信息地址
- ptrL findStu(ptrL p)
- {
- char num[10];
- printf("请输入学生学号:");
- scanf("%s", num);
- while (strcmp(num, p->num) != 0 && p->next != NULL)
- {
- p = p->next;
- }
- if (p->next == NULL && strcmp(num, p->num) != 0)
- {
- printf("学号输入错误,未查询到相关信息!\n");
- return NULL;
- }
- return p;
- }
- //更改学生信息
- void change(ptrL p)
- {
- ptrL p1;
- int a;
- p1 = findStu(p);
- if (p1 == NULL)
- {
- printf("更改失败!\n");
- return;
- }
- print(p1);
- printf("请选择需要更改的学生信息(1、学号 2、姓名 3、语文成绩 4、数学成绩 5、英语成绩):");
- scanf("%d", &a);
- printf("请输入替换信息:");
- if (a == 1)
- scanf("%s", p1->num);
- else if (a == 2)
- scanf("%s", p1->name);
- else
- scanf("%f", &p1->score[a - 3]);
- printf("更改信息成功!\n");
- return;
- }
- //删除学生信息
- ptrL deleteStu(ptrL p)
- {
- ptrL s = p, p1;
- p1 = findStu(p);
- if (p1 == NULL)
- {
- printf("删除失败!\n");
- return p;
- }
- print(p1);
- if (p1 == p)
- {
- p = p->next;
- if (count <= MAXSIZE)
- {
- ptrL temp = pool;
- pool = p1;
- pool->next = temp;
- count++;
- }
- else
- free(p1);
- printf("删除成功!\n");
- return p;
- }
- while (s->next != p1)
- {
- s = s->next;
- }
- s->next = p1->next;
- if (count <= MAXSIZE)
- {
- ptrL temp = pool;
- pool = p1;
- pool->next = temp;
- count++;
- }
- else
- free(p1);
- printf("删除成功!\n");
- return p;
- }
- //显示当前学生表信息
- void display(ptrL p)
- {
- if (p == NULL)
- {
- printf("列表为空,输出失败!\n");
- return;
- }
- int i = 1;
- while (p != NULL)
- {
- printf("第 %d 位学生信息:\n学号:%s\n姓名:%s\n", i, p->num, p->name);
- for (int j = 0; j < 3; j++)
- {
- if (j == 0)
- printf("语文");
- else if (j == 1)
- printf("数学");
- else
- printf("英语");
- printf("成绩:%.2f\n", p->score[j]);
- }
- p = p->next;
- i++;
- printf("\n");
- }
- return;
- }
- //退出
- void exitlist(ptrL p)
- {
- ptrL s;
- while (p != NULL)
- {
- s = p;
- p = p->next;
- free(s);
- }
- while (pool != NULL)
- {
- s = pool;
- pool = pool->next;
- free(s);
- }
- printf("程序结束!\n");
- exit(0);
- }
- int main(void)
- {
- int a;
- ptrL p = NULL;
- printf("**************学生信息表**************\n");
- while (1)
- {
- printf("请选择你需要进行的操作(1、添加学生信息 2、查找学生信息 3、更改学生信息 4、删除学生信息 5、显示当前列表 6、退出):");
- scanf("%d", &a);
- if (a == 1)
- p = addStu(p);
- else if (a == 2)
- {
- ptrL s;
- s = findStu(p);
- print(s);
- }
- else if (a == 3)
- change(p);
- else if (a == 4)
- deleteStu(p);
- else if (a == 5)
- display(p);
- else if (a == 6)
- exitlist(p);
- else
- printf("输入错误!\n");
- printf("\n");
- }
- return 0;
- }
复制代码
|
|