Y_寒酥 2023-01-11 21:38 采纳率: 87.5%
浏览 28
已结题

代码改错C语言数据结构学生信息删除

** 改错(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");//测试
 
}

img

```

  • 写回答

1条回答 默认 最新

  • 谛凌 2023-01-11 21:54
    关注

    struct student *p=phead->next;p->next=phead->next;
    这里的p->next=phead->next;是要做什么
    p=phead->next说明p已经指向第1个结点了
    p->next=phead->next;这一句又让p的next指向了p自己,也就是p一直在循环自己出不去,造成死循环了吧
    你可以把p->next=phead->next;删掉试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月29日
  • 已采纳回答 1月29日
  • 创建了问题 1月11日

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败