** 改错(C语言_数据结构单链表删除,学生管理系统)**
在写一个单链的增删时,增加没有问题,但是删除时报错:段错误,代码为switch case 3:,函数为void delete()那个函数部分
但是我改了很久并没有觉得有错误..,实在是找不出为什么
下面是源码:
```c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 5
struct student{
int num;
char name[N];
char sex[N];
float score1;
float score2;
float score3;
struct student *next;
}head;
struct student *phead=&head;
struct student *ptail=&head;
struct student stu2={1,"yao2","男",85.5,99,90.2,NULL};
struct student stu1={0,"yao1","男",85.5,86,90,&stu2};
//声明函数
void init(struct student *phead);
void travel(struct student *phead);
void add(struct student *phead);
void delete(struct student *phead);
//主函数
int main(){
int choice=0;
ptail->next=&stu1;phead->next=&stu1;
struct student *p=phead;
puts("-----------------------学生成绩管理系统---------------------");
puts("正在初始化..................");
init(phead);
loop:
puts("-----------------------学生成绩管理系统---------------------");
puts("请选择你的操作:");
printf("1.查看\n 2.增加\n 3.删除\n 4.刷新\n 5.结束退出\n");
scanf("%d",&choice);
switch(choice){
case 1:
travel(phead);goto loop;
case 2: add(phead);travel(phead);goto loop;
case 3:delete(phead);travel(phead);
case 4:goto loop;
case 5: printf("\n");return 0;
default:puts("error");goto loop;
}
printf("\n");
return 0;
}
void init(struct student *phead){
struct student *p=phead;
while(p){
printf("num:%d ,name:%s ,sex:%s ,score1:%.3f ,score2:%.3f ,score3:%.3f ,p:%p\n",p->num,p->name,p->sex,p->score1,p->score2,p->score3,p->next);
p=p->next;
}
}
void travel(struct student *phead){
struct student *p=phead->next;
while(p!=NULL){
printf("num:%d ,name:%s ,sex:%s ,score1:%.3f ,score2:%.3f ,score3:%.3f ,p:%p\n",p->num,p->name,p->sex,p->score1,p->score2,p->score3,p->next);
p=p->next;
}
printf("\n");
}
void add(struct student *phead){
struct student *p;
p=malloc(sizeof(*p));
p->next=NULL;
puts("请输入学生信息:num name sex score1 score2 score3");
scanf("%d %s %s %f %f %f",&p->num,p->name,p->sex,&p->score1,&p->score2,&p->score3);
while(ptail->next!=NULL){
ptail=ptail->next;
}
ptail->next=p;
ptail=ptail->next;
ptail->next=NULL;
// printf("num:%d ,name:%s ,sex:%s ,score1:%.3f ,score2:%.3f ,score3:%.3f ,ptail:%p\n",ptail->num,ptail->name,ptail->sex,ptail->score1,ptail->score2,ptail->score3,ptail->next);//测试
// free(p);//这段空间如果释放,那么存放改学生的信息将丢失
// printf("num:%d ,name:%s ,sex:%s ,score1:%.3f ,score2:%.3f ,score3:%.3f ,ptail:%p\n",ptail->num,ptail->name,ptail->sex,ptail->score1,ptail->score2,ptail->score3,ptail->next);//测试
printf("\n");
}
void delete(struct student *phead){
int numx=0;
struct student *p=phead->next;p->next=phead->next;
struct student *pre=phead;pre->next=phead->next;
//pre=phead;pre->next=phead->next;
puts("请输入要删除的学生信息的学号:");
scanf("%d",&numx);
while(p->num!=numx){
p=p->next;
pre=pre->next;
if(p=NULL){puts("未找到改学号,学生不存在");break;}
}
//找到了
if(p->num==numx){
pre->next=p->next;
pre=pre->next;
pre=phead;pre->next=phead->next;
//free(p);
}
#if 0
for(;;){
if(p->num==numx){pre->next=p->next;pre=pre->next;return 0;}
else if(p=NULL){puts("cannot find");break;}
else {p=p->next;pre=pre->next;}
}
#endif
printf("zhoa\n");//测试
}
```