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

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

img

#include 
#include 
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;
}

该回答引用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;
}