C++稀疏矩阵的十字链表

#include <iostream>
using namespace std;
struct MatNode;
struct uni
{
    int value;
    MatNode *link;
};
struct MatNode
{
    int row;
    int col;
    MatNode *right,*down;
    uni tag;


};
void CreatMat(MatNode*& mh ,char a[][4],int x,int y)
{
    MatNode *h[4],*p,*q,*r;
    mh=new MatNode;
    mh->row=x;
    mh->col=y;
    r=mh;
    for(int i=0; i<y; i++)
    {
        h[i]=new MatNode;
        h[i]->down=h[i]->right=h[i];
        r->tag.link=h[i];
        r=h[i];
    }
    r->tag.link=mh;
    for(int i=0; i<x; i++)
    {
        for(int j=0; j<y; j++)
        {
            if(a[i][j]!=0)
            {
                p=new MatNode;
                p->row=i;
                p->col=j;
                p->tag.value=a[i][j];
                q=h[i];
                while(q->right!=h[i]&&q->right->col<j)
                    q=q->right;
                p->right=q->right;
                q->right=p;
                q=h[j];
                while(q->down!=h[j]&&q->down->row<i)
                    q=q->down;
                p->down=q->down;
                q->down=p;
            }
        }
    }
}
void DisMat(MatNode* mh)
{
    MatNode* p,* q;
    cout<<"行="<<mh->row<<"列="<<mh->col<<endl;
    p=mh->tag.link;
    while(p!=mh)
    {
        q=p->right;
        while(p!=q)
        {
            cout<<q->row<<' '<<q->col<<' '<<q->tag.value<<endl;
            q=q->right;
        }
        p=p->tag.link;
    }
}
int main()
{
    MatNode *p;
    char a[3][4]= {{1,0,0,2},
        {0,0,3,0},
        {0,0,0,4}
    };
    CreatMat(p,a,3,4);
    DisMat(p);
    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值