C-OJ提交出现段错误?

lgqfhwy
  • 62

问题是在https://pta.patest.cn/pta/test/558/exam/4/question/8660
我自己写的代码自己运行正常,没有报错,但是提交的时候总是显示 段错误 ,不知道怎么回事,求大神指教。
运行出现的错误

#include <stdio.h>
#include <stdlib.h>

typedef struct _node {
    int Coefficient;
    int Exponent;
    struct _node *next;
} Node;

typedef struct _list {
    Node * head;
} List;
void insert(List * pList, int coeff, int expon);
void print(const List *pList);
void attach(int coeff, int expon, List * pList);
void add(List * list1, List * list2, List * listSum);
void multy(List * list1, List * list2, List * listProd);
int main()
{
    List list1, list2, listSum, listProd;
    list1.head = NULL;
    list2.head = NULL;
    listSum.head = NULL;
    listProd.head = NULL;
    int n, m;
    int i;
    int coeff, expon;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d %d", &coeff, &expon);
        insert(&list1, coeff, expon);
    }
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
        scanf("%d %d", &coeff, &expon);
        insert(&list2, coeff, expon);
    }
    multy(&list1, &list2, &listProd);
    print(&listProd);
    add(&list1, &list2, &listSum);
    print(&listSum);
    return 0;
}

void insert(List * pList, int coeff, int expon)
{
    Node *p = (Node *)malloc(sizeof(Node));
    p->Coefficient = coeff;
    p->Exponent = expon;
    p->next = NULL;
    Node *last = pList->head;
    if (last) {
        while (last->next) {
            last = last->next;  
        }
        last->next = p;   
    }
    else {
        pList->head = p;
    }
}

void print(const List *pList)
{
    Node *p;
    p = pList->head;
    if (!p) {
        printf("0 0\n");
        return ;
    }
    for (p = pList->head; p; p = p->next) {  
        //printf("hr1\n");   
        printf("%d %d", p->Coefficient, p->Exponent);
        if (p->next) {
            printf(" ");
        }
    }
    printf("\n");
}

void add(List * list1, List * list2, List * listSum)
{
    Node *last1 = list1->head;
    Node *last2 = list2->head;
    //List lastSum;
    //lastSum.head = listSum->head;
    while (last1 && last2) {
        //printf("hr1\n");
        if (last1->Exponent > last2->Exponent) {
            attach(last1->Coefficient, last1->Exponent, listSum);
            last1 = last1->next;
        }
        else if (last1->Exponent < last2->Exponent) {
            attach(last2->Coefficient, last2->Exponent, listSum);
            last2 = last2->next;
        }
        else {
            if (last1->Coefficient + last2->Coefficient )
                attach(last1->Coefficient + last2->Coefficient, last1->Exponent, listSum);
            
            last1 = last1->next;
            last2 = last2->next;
        }
    }
    for (; last1; last1 = last1->next) {
        //if last 
        attach(last1->Coefficient, last1->Exponent, listSum);
    }
    for (; last2; last2 = last2->next) {
        attach(last1->Coefficient, last2->Exponent, listSum);
    }
}

void attach(int coeff, int expon, List * pList)
{
    Node *p = (Node *)malloc(sizeof(Node));
    p->Coefficient = coeff;
    p->Exponent = expon;
    p->next = NULL;
    Node *last = pList->head;
    if (last) {
        while (last->next) {
            last = last->next;  
        }
        last->next = p;   
    }
    else {
        pList->head = p;
    }
}

void multy(List * list1, List * list2, List * listProd)
{
    if (!(list1->head) || !(list2->head)) {
        return ;
    }
    Node *last1 = list1->head;
    Node *last2 = list2->head;
    Node *lastProd = listProd->head;
    Node *t;
    int coeff, expon;

    for (; last1; last1 = last1->next) {
        attach(last1->Coefficient * last2->Coefficient, last1->Exponent + last2->Exponent, listProd);
    }
    //print(listProd);
    last2 = last2->next;
    //printf("hr3\n");
    for (; last2; last2 = last2->next) {
        lastProd = listProd->head;
        for (last1 = list1->head; last1; last1 = last1->next) {
            coeff = last1->Coefficient * last2->Coefficient;
            expon = last1->Exponent + last2->Exponent;
            //printf("hr4\n");
            while (lastProd->next && lastProd->next->Exponent > expon)
                lastProd = lastProd->next;
            if (lastProd->next && lastProd->next->Exponent == expon) {
                if (lastProd->next->Coefficient + coeff)
                    lastProd->next->Coefficient += coeff;
                else {
                    t = lastProd->next;
                    lastProd->next = t->next;
                    free(t);
                }
            }
            else {
                t = (Node *)malloc(sizeof(Node));
                t->Coefficient = coeff;
                t->Exponent = expon;
                t->next = lastProd->next;
                lastProd->next = t;
                lastProd = lastProd->next;
            }
        }
    }

}

我自己在dev gcc 4.9.2 运行均没有出现任何错误和warning.但是不知道在页面提交时就出现段错误了,不知道怎么回事,求大神指教:)

回复
阅读 3.9k
1 个回答
jaege
  • 2.5k
✓ 已被采纳

以下输入会出现段错误。

0
1 1 0

问题出在下面:

void add(List * list1, List * list2, List * listSum)
{
    /* ...... */
    for (; last2; last2 = last2->next) {
        attach(last1->Coefficient, last2->Exponent, listSum);
        //     ^^^^^ last1 可能为 NULL
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏