You need to enable JavaScript to run this app. C语言链表实现多项式的add函数运行异常,求调试并提供修复后完整代码
优惠活动
大模型
产品
解决方案
定价
更多

C语言链表实现多项式的add函数运行异常,求调试并提供修复后完整代码

C语言链表实现多项式相加bug修复说明

原有代码核心问题

  • 未保存多项式相加结果的头指针,最终打印时传入的是已经移动到链表尾部的指针,无法输出完整结果
  • 新节点分配的边界判断逻辑存在漏洞,会导致部分节点丢失或者生成多余空节点
  • 未处理系数相加为0的场景,会出现0xN这类无效项

修复后完整代码

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Node
{
    int coeff;
    int exp;
    struct Node *next;
} *poly = NULL, *poly2 = NULL;

void create()
{
    struct Node *t, *last = NULL;
    int num, i;
    printf("Enter number of terms: ");
    scanf("%d", &num);
    printf("Enter each term with coeff and exp\n");
    for (i = 0; i < num; i++)
    {
        t = (struct Node *)malloc(sizeof(struct Node));
        scanf("%d%d", &t->coeff, &t->exp);
        t->next = NULL;
        if (poly == NULL)
        {
            poly = last = t;
        }
        else
        {
            last->next = t;
            last = t;
        }
    }
}
void create2()
{
    struct Node *t, *last = NULL;
    int num, i;
    printf("Enter number of terms for second polynomial: ");
    scanf("%d", &num);
    printf("Enter each term with coeff and exp\n");
    for (i = 0; i < num; i++)
    {
        t = (struct Node *)malloc(sizeof(struct Node));
        scanf("%d%d", &t->coeff, &t->exp);
        t->next = NULL;
        if (poly2 == NULL)
        {
            poly2 = last = t;
        }
        else
        {
            last->next = t;
            last = t;
        }
    }
}

void Display(struct Node *p)
{
    if (p == NULL) {
        printf("0\n");
        return;
    }
    while (p)
    {
        if (p->next != NULL)
            printf("%dx%d + ", p->coeff, p->exp);
        else
            printf("%dx%d", p->coeff, p->exp);
        p = p->next;
    }
    printf("\n");
}
long Eval(struct Node *p, int x)
{
    long val = 0;
    while (p)
    {
        val += p->coeff * pow(x, p->exp);
        p = p->next;
    }
    return val;
}
void add(struct Node *p, struct Node *q)
{
    // 保存结果链表头指针,用于后续打印
    struct Node *sum_head = NULL, *sum_tail = NULL;
    while (p && q)
    {
        struct Node *t = (struct Node *)malloc(sizeof(struct Node));
        t->next = NULL;
        if (p->exp > q->exp)
        {
            t->exp = p->exp;
            t->coeff = p->coeff;
            p = p->next;
        }
        else if (p->exp < q->exp)
        {
            t->exp = q->exp;
            t->coeff = q->coeff;
            q = q->next;
        }
        else
        {
            t->exp = p->exp;
            t->coeff = p->coeff + q->coeff;
            q = q->next;
            p = p->next;
        }
        // 系数为0的项跳过不加入结果
        if (t->coeff == 0) {
            free(t);
            continue;
        }
        // 插入到结果链表尾部
        if (sum_head == NULL) {
            sum_head = sum_tail = t;
        } else {
            sum_tail->next = t;
            sum_tail = t;
        }
    }
    // 处理p剩余的节点
    while (p)
    {
        struct Node *t = (struct Node *)malloc(sizeof(struct Node));
        t->exp = p->exp;
        t->coeff = p->coeff;
        t->next = NULL;
        if (sum_head == NULL) {
            sum_head = sum_tail = t;
        } else {
            sum_tail->next = t;
            sum_tail = t;
        }
        p = p->next;
    }
    // 处理q剩余的节点
    while (q)
    {
        struct Node *t = (struct Node *)malloc(sizeof(struct Node));
        t->exp = q->exp;
        t->coeff = q->coeff;
        t->next = NULL;
        if (sum_head == NULL) {
            sum_head = sum_tail = t;
        } else {
            sum_tail->next = t;
            sum_tail = t;
        }
        q = q->next;
    }
    // 打印完整的结果链表
    Display(sum_head);
    // 释放结果链表内存避免泄漏,需要保留结果时可删除该段逻辑
    struct Node *tmp;
    while (sum_head) {
        tmp = sum_head;
        sum_head = sum_head->next;
        free(tmp);
    }
}
int main()
{
    create();
    create2();
    printf("First polynomial: ");
    Display(poly);
    printf("Second polynomial: ");
    Display(poly2);
    printf("Sum of polynomials: ");
    add(poly, poly2);
    // 可自行补充poly、poly2的内存释放逻辑避免泄漏
    return 0;
}

主要修改点

  • 新增sum_head指针保存结果链表首节点,确保打印时可以遍历完整结果
  • 重构节点分配逻辑,每次生成新项时单独分配节点,避免原有边界判断错误导致的节点丢失问题
  • 新增系数为0的过滤逻辑,避免相加后出现无效项
  • 优化Display函数输出格式,去掉末尾多余的加号
  • 新增空链表判断,避免多项式和为0时输出空内容

内容的提问来源于stack exchange,提问作者Mayank Singh Negi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 21:15:02