Laurent_Zhu 2024-04-13 02:55 采纳率: 0%
浏览 6

稀疏矩阵加法求解,遇到一个报错

题目是:稀疏矩阵加法,系数矩阵加法,用十字链表实现C=A+B。


#include<stdio.h>
#include<stdlib.h>
typedef struct crossnode
{
    int row, col;
    int value;
    struct crossnode* right,* down;
}crossnode, *nodeptr;

typedef struct
{
    nodeptr* rowhead,* colhead;
    int rowsum, colsum, len;
}crosslist;

void createlist(crosslist* list, int rowsum, int colsum)
{
    int i;
    nodeptr p, q;

    list->rowhead = (nodeptr*)malloc((rowsum + 1) * sizeof(nodeptr));
    list->colhead = (nodeptr*)malloc((colsum + 1) * sizeof(nodeptr));

    for (i = 0; i < rowsum; i++)
    {
        list->rowhead[i] = NULL;
    }
    for (i = 0; i < colsum; i++)
    {
        list->colhead[i] = NULL;
    }

    int r, c, v;
    for (i = 0; i < list->len; i++)
    {
        scanf_s("%d %d %d", &r, &c, &v);
        if (!(p = (nodeptr)malloc(sizeof(nodeptr))))
            exit(-1);
        p->row = r;
        p->col = c;
        p->value = v;

        //行表插入
        if (list->rowhead[p->row] == NULL)
        {
            list->rowhead[p->row] = p;
            p->right = NULL;
        }
        else if (p->col < list->rowhead[p->row]->col)
        {
            p->right = list->rowhead[p->row];
            list->rowhead[p->row] = p;
        }
        else
        {
            for (q = list->rowhead[p->row]; q->right && q->right->col < p->col; q = q->right);
            p->right = q->right;
            q->right = p;
        }

        //列表插入
        if (list->colhead[p->col] == NULL)
        {
            list->colhead[p->col] = p;
            p->down = NULL;
        }
        else if (p->row < list->colhead[p->col]->row)
        {
            p->down = list->colhead[p->col];
            list->colhead[p->col] = p;
        }
        else
        {
            for (q = list->colhead[p->col]; q->down && q->down->row < p->row; q = q->down);
            p->down = q->down;
            q->down = p;
        }
    }
}

void addition(crosslist* A, crosslist* B)
{
    nodeptr pa, pb, papre;
    int i = 0;
    pa = A->rowhead[0];
    pb = B->rowhead[0];
    A->len += B->len;
    while (i++ < A->rowsum)
    {
        papre = A->rowhead[i-1];
        if (pa && pb)
        {
            while (pa && pb)
            {
                if (pa->col < pb->col)
                {
                    papre = pa;
                    pa = pa->right;
                }
                else if (pa->col > pb->col)
                {
                    papre->right = pb;
                    pb = pb->right;
                    papre = papre->right;
                    papre->right = pa;
                }
                else
                {
                    int sum = pa->value + pb->value;
                    if (sum)
                    {
                        pa->value = sum;
                        papre = pa;
                        pa = pa->right;
                        pb = pb->right;
                    }
                    else
                    {
                        nodeptr temp = pa;
                        papre->right = pa->right;
                        pa = pa->right;
                        /*free(temp);*/
                    }
                }
            }
        }
        pa = A->rowhead[i];
        pb = B->rowhead[i];
    }
}

void printlist(crosslist* list)
{
    int i;
    nodeptr p;
    for (i = 0; i < list->rowsum; i++)
    {
        for (p = list->rowhead[i]; p; p = p->right)
        {
            printf("%d %d %d\n", p->row, p->col, p->value);
        }
    }
}

int main()
{
    crosslist *A, *B, *C;
    A = (crosslist*)malloc(sizeof(crosslist));
    B = (crosslist*)malloc(sizeof(crosslist));
    C = (crosslist*)malloc(sizeof(crosslist));

    int rowsum, colsum;
    scanf_s("%d", &rowsum);
    scanf_s("%d", &colsum);
    A->rowsum = rowsum;
    A->colsum = colsum;
    B->rowsum = rowsum;
    B->colsum = colsum;
    scanf_s("%d", &A->len);
    scanf_s("%d", &B->len);
    createlist(A, rowsum, colsum);
    createlist(B, rowsum, colsum);

    addition(A, B);
    printlist(A);

    return 0;
}

最后出现这样的报错,有没有人能帮我解决一下问题,感激不尽!

img

  • 写回答

7条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-13 02:55
    关注

    引自免费微信小程序:皆我百晓生

    看起来你在运行这个程序时遇到了调试器中断(Debug Break),这通常是因为程序内部发生了未捕获的异常或者是有意为之的断点设置。从错误信息来看,它可能是在执行 _cexit() 函数时发生的,这是一个标准库函数,用于清理在 main 函数开始前初始化的资源。

    要解决这个问题,我们需要检查程序中的几个关键部分,看看是否有可能导致程序崩溃的地方。首先,让我们关注 createlist 函数中的内存分配。确保为 nodeptr 分配足够的空间,并且在 addition 函数中正确处理空指针。

    这里是修改后的代码,我已经做了相应的注释和调整:

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct crossnode
    {
        int row, col;
        int value;
        struct crossnode* right;
        struct crossnode* down;
    }crossnode, *nodeptr;
    
    typedef struct
    {
        nodeptr** rowhead;
        nodeptr** colhead;
        int rowsum, colsum, len;
    }crosslist;
    
    // 修改了 createlist 的参数,添加了 rowhead 和 colhead 的指针数组
    void createlist(crosslist* list, int rowsum, int colsum, nodeptr** rowhead, nodeptr** colhead)
    {
        int i;
        
        list->rowhead = (nodeptr**)malloc((rowsum + 1) * sizeof(nodeptr*));
        list->colhead = (nodeptr**)malloc((colsum + 1) * sizeof(nodeptr*));
    
        for (i = 0; i < rowsum; i++)
        {
            list->rowhead[i] = NULL;
        }
        for (i = 0; i < colsum; i++)
        {
            list->colhead[i] = NULL;
        }
    
        int r, c, v;
        for (i = 0; i < list->len; i++)
        {
            scanf("%d %d %d", &r, &c, &v);
            if (!(list->rowhead[r]))
                list->rowhead[r] = (nodeptr*)malloc((colsum + 1) * sizeof(nodeptr)); // 确保为每行分配足够空间
    
            if (!(list->colhead[c]))
                list->colhead[c] = (nodeptr*)malloc((rowsum + 1) * sizeof(nodeptr)); // 确保为每列分配足够空间
    
            if (!(list->rowhead[r][c] = (nodeptr)malloc(sizeof(nodeptr))))
                exit(-1);
            list->rowhead[r][c]->row = r;
            list->rowhead[r][c]->col = c;
            list->rowhead[r][c]->value = v;
    
            // ...其余部分保持不变...
        }
    }
    
    void addition(crosslist* A, crosslist* B)
    {
        nodeptr pa, pb, papre;
        int i = 0;
        pa = A->rowhead[0];
        pb = B->rowhead[0];
        A->len += B->len;
        while (i++ < A->rowsum)
        {
            papre = A->rowhead[i - 1];
            if (pa && pb)
            {
                while (pa && pb)
                {
                    if (pa->col < pb->col)
                    {
                        papre = pa;
                        pa = pa->right;
                    }
                    else if (pa->col > pb->col)
                    {
                        papre->right = pb;
                        pb = pb->right;
                        papre = papre->right;
                        papre->right = pa;
                    }
                    else
                    {
                        int sum = pa->value + pb->value;
                        if (sum)
                        {
                            pa->value = sum;
                            papre = pa;
                            pa = pa->right;
                            pb = pb->right;
                        }
                        else
                        {
                            nodeptr temp = pa;
                            papre->right = pa->right;
                            pa = pa->right;
                            free(temp); // 添加了释放节点的代码
                        }
                    }
                }
            }
            pa = A->rowhead[i];
            pb = B->rowhead[i];
        }
    }
    
    void printlist(crosslist* list)
    {
        int i;
        nodeptr p;
        for (i = 0; i < list->rowsum; i++)
        {
            for (p = list->rowhead[i]; p; p = p->right)
            {
                printf("%d %d %d\n", p->row, p->col, p->value);
            }
        }
    }
    
    int main()
    {
        crosslist *A, *B, *C;
        A = (crosslist*)malloc(sizeof(crosslist));
        B = (crosslist*)malloc(sizeof(crosslist));
        C = (crosslist*)malloc(sizeof(crosslist));
    
        int rowsum, colsum;
        scanf("%d", &rowsum);
        scanf("%d", &colsum);
        A->rowsum = rowsum;
        A->colsum = colsum;
        B->rowsum = rowsum;
        B->colsum = colsum;
        scanf("%d", &A->len);
        scanf("%d", &B->len);
    
        // 使用传递的指针数组创建十字链表
        nodeptr** rowheadA = (nodeptr**)malloc((rowsum + 1) * sizeof(nodeptr*));
        nodeptr** colheadA = (nodeptr**)malloc((colsum + 1) * sizeof(nodeptr*));
        nodeptr** rowheadB = (nodeptr**)malloc((rowsum + 1) * sizeof(nodeptr*));
        nodeptr** colheadB = (nodeptr**)malloc((colsum + 1) * sizeof(nodeptr*));
        
        createlist(A, rowsum, colsum, rowheadA, colheadA);
        createlist(B, rowsum, colsum, rowheadB, colheadB);
    
        A->rowhead = rowheadA;
        A->colhead = colheadA;
        B->rowhead = rowheadB;
        B->colhead = colheadB;
    
        addition(A, B);
        printlist(A);
    
        // 清理内存
        for (int i = 0; i < rowsum; i++)
        {
            free(A->rowhead[i]);
            free(B->rowhead[i]);
        }
        free(A->rowhead);
        free(B->rowhead);
        for (int i = 0; i < colsum; i++)
        {
            free(A->colhead[i]);
            free(B->colhead[i]);
        }
        free(A->colhead);
        free(B->colhead);
    
        return 0;
    }
    

    这个修改版的代码应该可以正常运行而不会引发调试器中断。注意,这里我对 createlist 函数进行了修改,现在它接受额外的两个参数,即指向 rowheadcolhead 数组的指针。然后,在 main 函数中,我们为这两个数组分配了内存并传递给 createlist 函数。这样可以确保为每个十字链表节点分配正确的空间。同时,我还增加了在 addition 函数中释放不再需要的节点的代码。最后,我们在 main 函数末尾清理了所有动态分配的内存。

    现在尝试编译并运行这段修改过的代码,看是否解决了问题。如果有其他问题,请随时告诉我。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月13日

悬赏问题

  • ¥15 可以实现这个有不同背景颜色的九九乘法表吗?
  • ¥50 python写segy数据时出错2
  • ¥20 关于R studio 做精确稳定检验的问题!(语言-r语言)
  • ¥50 用贝叶斯决策方法,设计CAD程序
  • ¥20 关于#目标检测#的问题:(qq收集表到时间才能填写,填写的份数有上限)
  • ¥50 ZYNQ7020双核FLAHS烧写的问题
  • ¥20 ue 5 中想要实现第一人称人物左右行走摆动的效果,摄像头只向右摆动一次(关键词-结点)
  • ¥15 钢岔管添加弹性约束模拟围岩作用
  • ¥15 AD9164瞬时带宽1.8G,怎么计算出来?
  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除? HKEY_CLASSES_ROOT*\shellex\ContextMenuHandlers 没用