用十字链表实现矩阵加法

题目复制粘贴


感谢https://blog.youkuaiyun.com/npuyan/article/details/80464459?spm=1001.2014.3001.5501
这位代码写的很好

细节

创建十字链表并不是非常难。我借鉴了这位(https://blog.youkuaiyun.com/zhuyi2654715/article/details/6729783)的写法,写的很清楚,但是不能实现同一处连续插入两次的操作。
最难的还是加法。我们假设把B加到A上面,那么,就存在添加新结点,不加不减(做加法未得到0),删除结点(做加法得到0)三种情况。对于删除结点呢,我们先从水平方向遍历,确定要删的结点——但是还不能删除(否则垂直方向会导致野指针出现),所以只能先标记,等之后垂直方向遍历时再删掉。
更糟糕的是,由于我们创建十字链表为了节省空间,没有采用头结点而是头指针,所以对于某一行或者某一列只有一个结点的情况,还要单独处理(自闭),这时候,有一个前驱指针就显得极其重要了。这是我大意没有闪的一处

代码

#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct node
{
    int i,j,v;
    struct node *right,*down;
}OLNode,*OList;
typedef struct
{
    OList *rhead,*chead;
    int mu,nu,tu;
}CrossList;

void Create_CrossList(CrossList *L,int m,int n,int t)
{
    L->chead=(OList*)malloc(sizeof(OList)*(m+1));
    if(!L->chead)
    {
        printf("十字链表指针数组分配空间失败\n");
        exit(-1);
    }
    L->rhead=(OList*)malloc(sizeof(OLNode)*(n+1));
    if(!L->rhead)
    {
        printf("十字链表指针数组分配空间失败\n");
        exit(-1);
    }
    L->mu=m,L->nu=n,L->tu=t;
    int i;
    for(i=1;i<=m;i++)
    {
        L->rhead[i]=NULL;
    }
    for(i=1;i<=n;i++)///初始化指针数组
    {
        L->chead[i]=NULL;
    }
    for(i=1;i<=t;i++)
    {
        int r,c,v;
        scanf("%d%d%d",&r,&c,&v);
        OList node=(OList)malloc(sizeof(OLNode));
        if(!node)
        {
            printf("结点内存分配失败\n");
            exit(-1);
        }
        node->i=r,node->j=c,node->v=v;
        if(L->rhead[r]==NULL||L->rhead[r]->j>c)///可以直接插在开头
        {
            node->right=L->rhead[r];
            L->rhead[r]=node;
        }
        else
        {
            OList temp;
            for(temp=L->rhead[r];temp->right&&temp->right->j<c;temp=temp->right);
            ///寻找插入位置
            node->right=temp->right;
            temp->right=node;
        }
        if(L->chead[c]==NULL||L->chead[c]->i>r)///可以直接插在开头
        {
            node->down=L->chead[c];
            L->chead[c]=node;
        }
        else
        {
            OList temp;
            for(temp=L->chead[c];temp->down&&temp->down->i<r;temp=temp->down);
            ///寻找插入位置
            node->down=temp->down;
            temp->down=node;
        }
    }
}

void Insert(CrossList *A,OList node)
{
    int r=node->i,c=node->j,v=node->v;
    int del=0,added=0;///del表示要不要删结点,added表示要不要加入结点
    if(A->rhead[r]==NULL||A->rhead[r]->j>c)///直接插在开头
    {
        node->right=A->rhead[r];
        A->rhead[r]=node;
        added=1;
        A->tu++;
    }
    else
    {
        OList temp=A->rhead[r],pre=temp;
        while(temp&&temp->j<c)///找到插入位置。保留前驱pre非常有用。
        {
            pre=temp;
            temp=temp->right;
        }
        if(!temp)///因为第r行至少有一个结点,则temp为NULL说明pre->j<c,需要插入结点
        {
            pre->right=node;
            A->tu++;
            added=1;
        }
        else if(temp->j==c)///等于要分情况:有一个结点和有多个结点
        {
            temp->v+=v;
            if(!temp->v)
            {
                del=1;
                A->tu--;
                if(pre==temp)///我们只有头指针,没有头结点,所以pre==temp这里要特殊处理
                {
                    A->rhead[r]=temp->right;///有一个结点
                }
                else
                {
                    pre->right=temp->right;///有多个结点
                }
            }
        }
        else if(temp->j>c)///这一段不能省去。
        {
            added=1;
            A->tu++;
            node->right=temp,pre->right=node;
        }
    }
    if(A->chead[c]==NULL||A->chead[c]->i>r)///直接插在开头
    {
        node->down=A->chead[c];
        A->chead[c]=node;
    }
    else
    {
        OList temp=A->chead[c],pre=temp;
        while(temp&&temp->i<r)///找到插入位置。保留前驱pre非常有用。
        {
            pre=temp;
            temp=temp->down;
        }
        if(added==1)///需要插入
        {
            node->down=temp;
            pre->down=node;
        }
        else if(del==1)///需要删除
        {
            pre->down=temp->down;
            free(temp),free(node);
        }
        else if(!added&&!del) free(node);
    }
}
void add(CrossList *A,CrossList *B)
{
    OList now;
    int k;
    for(k=1;k<=B->mu;k++)
    {
        now=B->rhead[k];
        while(now)
        {
            OList temp=(OList)malloc(sizeof(OLNode));
            temp->i=now->i,temp->j=now->j,temp->v=now->v;
            temp->down=now->down,temp->right=now->right;///做一份拷贝,因为插入时涉及down和right域的改变
            Insert(A,temp);
            now=now->right;
        }
    }
}
void print_result(CrossList *A)
{
    int k;
    for(k=1;k<=A->mu;k++)
    {
        OList temp=A->rhead[k];
        while(temp)
        {
            printf("%d %d %d\n",temp->i,temp->j,temp->v);
            temp=temp->right;
        }
    }
}
int main()
{
    CrossList *A=(CrossList*)malloc(sizeof(CrossList));
    CrossList *B=(CrossList*)malloc(sizeof(CrossList));
    int m,n,t1,t2;
    scanf("%d%d%d%d",&m,&n,&t1,&t2);
    Create_CrossList(A,m,n,t1);///创建十字链表
    Create_CrossList(B,m,n,t2);
    add(A,B);///加法
    print_result(A);///输出结果
    free(A),free(B);
    return 0;
}
/*
3 4 3 2
1 1 1
1 3 1
2 2 2
1 2 1
2 2 3
*/
/*
3 4 3 2
1 1 4
2 2 3
3 3 2
1 1 -4
2 2 -3
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值