teio931226 2023-03-15 14:01 采纳率: 100%
浏览 21
已结题

关于#链表#的问题,调试时出现Segmentetion fault,如何解决?

刚学到单链表,设计输出多项式的函数时出现了Segmentetion fault报错,查了一下好像是因为指针没有初始化,但是不知道是哪个指针没有初始化(按理说应该都初始化过了才对

img

#include <stdio.h>
#include <stdlib.h>
struct node{
    int c;//系数
    int e;//指数
    struct node *next=NULL;
};
void read(node *head){
    head=(struct node*) malloc (sizeof(node));
    head->next=NULL;
    node *q;
    int c,e;
    while(1){
        scanf("%d %d",&c,&e);
        if(c==0){
            return;
        }
        else{
            q=(struct node*) malloc (sizeof(node));
            q->c=c;
            q->e=e;
            q->next=head->next;
            head->next=q;
            head=q;
        }
    }
}//读入多项式
void print(node *head){
    if(head->next->e==0){
        printf("%d",head->next->c);
    }
    else{
        printf("%dx^%d",head->next->c,head->next->e);
    }//先输出第一项
    head=head->next;
    while(head->next!=NULL){
        if(head->next->c>0){
            if(head->next->e==0){
                printf("+%d",head->next->c);
            }
            else{
                printf("+%dx^%d",head->next->c,head->next->e);
            }
        }
        else if(head->next->c<0){
            if(head->next->e==0){
                printf("%d",head->next->c);
            }
            else{
                printf("%dx^%d",head->next->c,head->next->e);
            }
        }
        else;
        head=head->next;
    }
}//输出多项式
int main(){
    node *f,*g,*h;
    read(f);
    read(g);
    print(f);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • MarkHan_ 2023-03-15 14:10
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ
    在该代码中,出现Segmentation fault错误的原因是在read函数中对参数head指针的处理不正确,具体来说,head是一个指向结构体node的指针,而在read函数中,head是以传值的方式传入的,因此head指针的修改只会在read函数内部生效,不会影响到main函数中head指针的值。因此,在read函数中对head指针的修改不会对main函数中head指针的值产生影响,也就导致了Segmentation fault错误。

    解决该问题的方法有两种:

    1.将head指针改为指向指针的指针,以使得read函数中对head指针的修改可以影响到main函数中head指针的值。

    2.在read函数中使用局部变量,将读入的多项式构建成链表,并返回链表的头指针,在main函数中再将头指针赋值给f和g。

    以下是第一种方法的修改代码示例:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
        int c;//系数
        int e;//指数
        struct node *next;
    };
    
    void read(struct node **head){
        *head = (struct node*) malloc (sizeof(struct node));
        (*head)->next = NULL;
        struct node *q, *p = *head;
        int c, e;
        while (1) {
            scanf("%d %d",&c,&e);
            if (c == 0) {
                break;
            }
            q = (struct node*) malloc (sizeof(struct node));
            q->c = c;
            q->e = e;
            q->next = NULL;
            p->next = q;
            p = q;
        }
    }
    
    void print(struct node *head){
        if(head->next->e == 0){
            printf("%d",head->next->c);
        }
        else{
            printf("%dx^%d",head->next->c,head->next->e);
        }//先输出第一项
        head = head->next;
        while(head->next != NULL){
            if(head->next->c > 0){
                if(head->next->e == 0){
                    printf("+%d",head->next->c);
                }
                else{
                    printf("+%dx^%d",head->next->c,head->next->e);
                }
            }
            else if(head->next->c < 0){
                if(head->next->e == 0){
                    printf("%d",head->next->c);
                }
                else{
                    printf("%dx^%d",head->next->c,head->next->e);
                }
            }
            else;
            head = head->next;
        }
    }
    
    int main(){
        struct node *f = NULL, *g = NULL, *h = NULL;
        read(&f);
        read(&g);
        print(f);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?