西北工业大学数据结构noj2-3

该代码示例展示了如何使用C++中的十字链表数据结构来实现两个稀疏矩阵的加法操作。程序首先创建两个稀疏矩阵,然后进行矩阵加法,并显示结果。稀疏矩阵用于高效存储大量为零的元素。
摘要由CSDN通过智能技术生成

2-3十字链表实现稀疏矩阵加法

question

由于显示问题,问号为空格

完整代码


// 十字链表实现稀疏矩阵加法

#include <iostream>
#include <vector>
#include <iomanip>
using DataType = int;

struct TridaNode
{
    DataType val;
    int row, col;
    TridaNode *right, *down;
};

using TridaList = TridaNode *;

struct CrossNode
{
    TridaList *row_head, *col_head;
    int m, n, len; // 行、列、非零元素
};
using CrossList = CrossNode *;

void CreatCrossList(CrossNode &Mat);

void InsertNode(CrossNode &Mat, int row, int col, DataType val);

void AddCrossList(CrossNode A, CrossNode B, CrossNode &C);

void showSparseMatrix(CrossNode Mat);

void SparsetoNormal(CrossNode Mat);

int main()
{
    CrossNode A, B, C;
    int m, n, t1, t2;
    std::cin >> m >> n >> t1 >> t2;

    A.m = B.m = m, A.n = B.n = n, A.len = t1, B.len = t2;
    CreatCrossList(A);
    // std::cout << "-----------------" << std::endl;
    CreatCrossList(B);
    // std::cout << "-----------------" << std::endl;
    // showSparseMatrix(A);
    // std::cout << "-----------------" << std::endl;
    // showSparseMatrix(B);
    // std::cout << "-----------------" << std::endl;

    AddCrossList(A, B, C);
    showSparseMatrix(C);
    // std::cout << "-----------------" << std::endl;
    // std::cout << "--------------------------------------" << std::endl;
    // std::cout << "A:" << std::endl;
    // SparsetoNormal(A);

    // std::cout << "--------------------------------------" << std::endl;
    // std::cout << "B:" << std::endl;
    // SparsetoNormal(B);

    // std::cout << "--------------------------------------" << std::endl;
    // std::cout << "C:" << std::endl;
    // SparsetoNormal(C);
    system("pause");
    return 0;
}

void CreatCrossList(CrossNode &Mat)
{
    Mat.col_head = new TridaList[Mat.n + 1]{nullptr};
    Mat.row_head = new TridaList[Mat.m + 1]{nullptr};
    if (!Mat.col_head || !Mat.row_head)
    {
        std::cout << "分配内存失败!" << std::endl;
        return;
    }
    // Mat.row_head[Mat.m] = {0}, Mat.col_head[Mat.n + 1] = 0;

    int row, col;
    DataType val;
    for (auto i = 0; i < Mat.len; ++i)
    {
        std::cin >> row >> col >> val;

        InsertNode(Mat, row, col, val);
        // TridaList p = new TridaNode;
        // if (!p)
        // {
        //     std::cout << "分配内存失败!" << std::endl;
        //     return;
        // }

        // p->row = row, p->col = col, p->val = val, p->right = nullptr, p->down = nullptr;
        // if (!Mat.row_head[row])
        //     Mat.row_head[row] = p;
        // else
        // {
        //     TridaList q = Mat.row_head[row];
        //     while (q->right && q->right->col < col)
        //         q = q->right;
        //     p->right = q->right, q->right = p;
        // }
        // if (!Mat.col_head[col])
        //     Mat.col_head[col] = p;
        // else
        // {
        //     TridaList q = Mat.col_head[col];
        //     while (q->down && q->down->row < row)
        //         q = q->down;
        //     p->down = q, q->down = p;
        // }
    }
}

void InsertNode(CrossNode &Mat, int row, int col, DataType val)
{
    TridaList p = new TridaNode;
    if (!p)
    {
        std::cout << "分配内存失败!" << std::endl;
        return;
    }

    p->row = row, p->col = col, p->val = val, p->right = nullptr, p->down = nullptr;
    if (!Mat.row_head[row])
        Mat.row_head[row] = p;
    else
    {
        TridaList q = Mat.row_head[row];
        while (q->right && q->right->col < col)
            q = q->right;
        p->right = q->right, q->right = p;
    }

    if (!Mat.col_head[col])
        Mat.col_head[col] = p;
    else
    {
        TridaList q = Mat.col_head[col];
        while (q->down && q->down->row < row)
            q = q->down;
        p->down = q->down, q->down = p;
    }
}

void AddCrossList(CrossNode A, CrossNode B, CrossNode &C)
{
    int size = 0;
    if (A.m != B.m || A.n != B.n)
    {
        std::cout << "输入矩阵不合法!" << std::endl;
        return;
    }

    C.m = A.m, C.n = A.n;
    C.col_head = new TridaList[C.n + 1]{nullptr};
    C.row_head = new TridaList[C.m + 1]{nullptr};

    for (auto i = 0; i < A.m; ++i)
    {
        TridaList A_temp = A.row_head[i], B_temp = B.row_head[i];

        if (!B_temp)
        {
            while (A_temp)
            {
                InsertNode(C, A_temp->row, A_temp->col, A_temp->val);
                ++size;
                A_temp = A_temp->right;
            }
        }
        else
        {
            if (!A_temp)
            {
                while (B_temp)
                {
                    InsertNode(C, B_temp->row, B_temp->col, B_temp->val);
                    ++size;
                    B_temp = B_temp->right;
                }
            }

            else
            {
                while (A_temp && B_temp)
                {
                    if (A_temp->row < B_temp->row || (A_temp->row == B_temp->row && A_temp->col < B_temp->col))
                    {
                        InsertNode(C, A_temp->row, A_temp->col, A_temp->val);
                        ++size;
                        A_temp = A_temp->right;
                    }
                    else if (B_temp->row < A_temp->row || (B_temp->row == A_temp->row && B_temp->col < A_temp->col))
                    {
                        InsertNode(C, B_temp->row, B_temp->col, B_temp->val);
                        ++size;
                        B_temp = B_temp->right;
                    }
                    else
                    {
                        int sum = A_temp->val + B_temp->val;
                        if (sum != 0)
                        {
                            InsertNode(C, A_temp->row, A_temp->col, sum);
                            ++size;
                        }
                        A_temp = A_temp->right;
                        B_temp = B_temp->right;
                    }
                }
                while (A_temp)
                {
                    InsertNode(C, A_temp->row, A_temp->col, A_temp->val);
                    ++size;
                    A_temp = A_temp->right;
                }
                while (B_temp)
                {
                    InsertNode(C, B_temp->row, B_temp->col, B_temp->val);
                    ++size;
                    B_temp = B_temp->right;
                }
            }
        }
    }
    C.len = size;
}

void showSparseMatrix(CrossNode Mat)
{
    for (auto i = 0; i < Mat.m; ++i)
    {
        TridaList p = Mat.row_head[i];
        while (p)
        {
            std::cout << p->row << " " << p->col << " " << p->val << std::endl;
            p = p->right;
        }
    }
}

void SparsetoNormal(CrossNode Mat)
{
    std::vector<std::vector<int>> temp(Mat.m, std::vector<int>(Mat.n, 0));
    for (auto i = 0; i < Mat.m; ++i)
    {
        TridaList p = Mat.row_head[i];
        while (p)
        {
            temp[p->row][p->col] = p->val;
            p = p->right;
        }
    }

    for (auto i = 0; i < Mat.m; ++i)
    {
        for (auto j = 0; j < Mat.n; ++j)
        {
            std::cout << std::setw(3) << temp[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

// 3 4 3 2
// 1 1 1
// 1 3 1
// 2 2 2
// 1 2 1
// 2 2 3
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值