#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;
}
C++稀疏矩阵的十字链表
最新推荐文章于 2024-01-12 09:24:24 发布