读书人

急新手关于十字链表进行矩阵相加

发布时间: 2012-02-25 10:01:49 作者: rapoo

急!!!新手求救,关于十字链表进行矩阵相加
(1)采用十字链表存储两个稀疏矩阵A和B。
(2)求出C=A+B,输出C。

我的程序:

#include<iostream>
using namespace std;

#define M 4
#define N 4
#define Max 16

typedef int datatype;
typedef struct mtxn{
int row;
int col;
struct mtxn *rp,*dp;
union{
datatype value;
struct mtxn *link;
}tag;
}Mat;


void creat(Mat *&mh,datatype a[M][N])
{
int i,j;
Mat *h[Max];
Mat *p,*q,*r;
mh=(Mat *)malloc(sizeof(Mat));
mh->row=M;
mh->col=N;
r=mh;
for(i=0;i<Max;i++)
{
h[i]=(Mat *)malloc(sizeof(Mat));
h[i]->row=h[i]->col=-1;
h[i]->rp=h[i];
h[i]->dp=h[i];
r->tag.link=h[i];
r=h[i];
}
r->tag.link=mh;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(a[i][j]!=0)
{
p=(Mat *)malloc(sizeof(Mat));
p->row=i;
p->col=j;
p->tag.value=a[i][j];
q=h[i];
while(q->rp!=h[i]&&q->rp->col<j)
q=q->rp;
p->rp=q->rp;
q->rp=p;
q=h[i];
while(q->dp!=h[i]&&q->dp->row<i)
q=q->dp;
p->dp=q->dp;
q->dp=p;
}
}
}
}


void insert(int i,int j,int v,Mat *h[])
{
Mat *p,*q;
p=(Mat *)malloc(sizeof(Mat));
p->row=i;
p->col=j;
p->tag.value=v;
q=h[i];
while(q->rp!=h[i]&&q->rp->col<j)
q=q->rp;
p->rp=q->rp;
q->rp=p;
q=h[i];
while(q->dp!=h[i]&&q->dp->row<i)
q=q->dp;
p->dp=q->dp;
q->dp=p;
}


void print(Mat *mh)
{
Mat *p,*q;
cout<<"行="<<mh->row<<" 列="<<mh->col<<endl;
p=mh->tag.link;
while(p!=mh)
{
q=p->rp;
while(p!=q)
{
cout<<q->row<<""<<q->col<<" "<<q->tag.value<<endl;
q=q->rp;
}
p=p->tag.link;
}
}


Mat *add(Mat *A,Mat *B,Mat *C)
{
Mat *p,*q,*s,*t,*r,*h[Max];
int tem,i;
if(A->row!=B->row||A->col!=B->col)
cout<<"无法进行相加"<<endl;
else
{
C=(Mat *)malloc(sizeof(Mat));
C->row=A->row;
C->col=A->col;
if(C->row>C->col)
tem=C->row;
else
tem=C->col;
h[0]=C;
for(i=1;i<=tem;i++)
{
r=(Mat *)malloc(sizeof(Mat));
r->row=r->col=-1;
r->rp=r;
r->dp=r;
h[i]=r;
h[i-1]->tag.link=r;
}
h[tem]->tag.link=C;
p=A->tag.link;
s=B->tag.link;
while(p!=A&&s!=B)
{
q=p->rp;
t=s->rp;
if(q==p&&t!=s)
while(t!=s)
{
insert(t->row,t->col,t->tag.value,h);
t=t->rp;
}
else
if(s==t&&q!=p)
{
while(q!=p)
{
insert(q->row,q->col,q->tag.value,h);
q=q->rp;
}
}
else
if(q!=p&&t!=s)
{
while(q!=p&&t!=s)
{
if(q->col<t->col)
{
insert(q->row,q->col,q->tag.value,h);
q=q->rp;
}
else
if(q->col>t->col)
{
insert(t->row,t->col,t->tag.value,h);
t=t->rp;
}
else
{
if(q->tag.value+t->tag.value!=0)
insert(q->row,q->col,(q->tag.value+t->tag.value),h);


q=q->rp;
t=t->rp;
}
}
if(q==p&&t!=s)
while(t!=s)
{
insert(t->row,t->col,t->tag.value,h);
t=t->rp;
}
else
if(t==s&&q!=p)
while(q!=p)
{
insert(q->row,q->col,q->tag.value,h);
q=q->rp;
}
}
else
{
p=p->tag.link;
s=s->tag.link;
}
}
}
return C;
}


void main()
{
Mat *A,*B,*C;
A=(Mat *)malloc(sizeof(Mat));
B=(Mat *)malloc(sizeof(Mat));
int i,j;
int a[M][N],b[M][N];
cout<<"请输入矩阵A"<<endl;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
cin>>a[i][j];
creat(A,a);
cout<<"A :"<<endl;
print(A);

cout<<endl<<"请输入矩阵B"<<endl;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
cin>>b[i][j];
creat(B,b);
cout<<"B :"<<endl;
print(B);

cout<<"A与B相加"<<endl;

C=add(A,B,C);
cout<<"C :"<<endl;
print(C);
}

总是指出在insert()的
while(q->rp!=h[i]&&q->rp->col<j)
q=q->rp;
有问题,我实在不知道改怎么解决,求能人帮帮忙了!!谢谢大家了!!!

[解决办法]
指针为空没判断。
如果插入的地方在行末,q会取到null的。这个时候q->xxx肯定出错。

读书人网 >C++

热点推荐