这是我写的十字链表的相加的,但在输出成距阵的时候有问题。我不知道该什么整。跪求哪个高手帮忙解决一下!小弟不胜感激·(下面就是我的程序 C语言的)
#include <stdio.h>
#include <stdlib.h>
#define max 100
struct matnode
{
int row,col;
struct matnode *right,*down;
union {
int val;//val—数据域
struct matnode *next;
}tag;
};
struct matnode *createmat( struct matnode *h[])//*h是十字链表每行的表头指针数组
{
int m, n, t, s, i, r, c, v;
struct matnode *p,*q;
printf("行数m,列数n,非零元个数t:\n");
scanf("%d, %d, %d", &m, &n, &t);
p=(struct matnode*)malloc (sizeof (struct matnode));//建立表头
h[0]=p;
p->row=m;
p->col=n;
s=m>n?m:n;//s为m,n中的较大数
for(i=1;i<=s;i++)
{
p=(struct matnode*)malloc (sizeof (struct matnode));
h[i]=p;
h[i-1]->tag.next=p;
p->row=p->col=0;
p->down=p->right=p;//p->down为p的向下域,right为右域
}
h[s]->tag.next=h[0];
for(i=1;i<=t;i++) //t为非零元个数
{
printf("\t第%d个元素(行号r ,列号c, 值v):",i);
scanf("%d, %d, %d", &r, &c, &v);
p=(struct matnode*)malloc (sizeof (struct matnode));
p->row=r;
p->col=c;
p->tag.val=v;
q=h[r];
while (q->right!=h[r] && q->right->col<c)
q=q->right;
p->right=q->right;
q->right=p;
q=h[c];
while (q->down!=h[c] && q->down->row<r)
q=q->down;
p->down=q->down;
q->down=p;
}
return (h[0]);
}
void prmat (struct matnode *hm)//输出十字链表
{
struct matnode *p,*q;
int m,n,k=1;
printf("\n相加以后按行表输出距阵元素:\n");
printf("row=%d col=%d\n",hm->row,hm->col);
p=hm->tag.next;
for(m=1;m<=hm->row;m++)
for(n=1;n<=hm->col;n++)
{
while (p!=hm)
{
q=p->right;
while (p!=q)
{
if(m==q->row && n==q->col)
{printf(" %-2d",q->tag.val);
k++;
}
else {
printf(" 0");
k++;
}
if(k%hm->row) printf("\n");
q=q->right;
}
p=p->tag.next;
}
}
}
struct matnode *colpred(i,j,h)//根据行号i和列号j找出距阵第i行第j列的非零元在十字链表中的前驱结点
int i,j;
struct matnode *h[];
{
struct matnode *d;
d=h[j];
while (d->down->col!=0 && d->down->row<i)
d=d->down;
return (d);
}
struct matnode *addmat(ha,hb,h)
struct matnode *ha,*hb,*h[];
{
struct matnode *p,*q,*ca,*cb,*pa,*pb,*qa;
if(ha->row!=hb->row || ha->col!=hb->col)
{
printf("两个距阵的类型不同,不能相加\n");
exit(0);
}
else
{
ca=ha->tag.next;
cb=hb->tag.next;
do
{
pa=ca->right;
pb=cb->right;
qa=ca;
while (pb->col!=0)
if (pa->col<pb->col && pa->col!=0)
{
qa=pa;
pa=pa->right;
}
else if (pa->col>pb->col || pa->col==0)
{
p=(struct matnode*)malloc (sizeof (struct matnode));
*p=*pb;
p->right=pa;
qa->right=p;
qa=p;
q=colpred(p->row,p->col,h);
p->down=q->down;
q->down=p;
pb=pb->right;
}
else
{
pa->tag.val+=pb->tag.val;
if(pa->tag.val==0)
{
qa->right=pa->right;
q=colpred(pa->row,pa->col,h);
q->down=pa->down;
free(pa);
}
else qa=pa;
pa=pa->right;
pb=pb->right;
}
ca=ca->tag.next;
cb=cb->tag.next;
}while(ca->row==0);
}
return (h[0]);
}
//元素查找
int findmat(struct matnode *hm ,int x,int *rown, int *coln)
{
struct matnode *p,*q;
p=hm->tag.next;
while (p!=hm)
{
q=p->right;
while (p!=q)
{
if(q->tag.val==x)
{
*rown=q->row;
*coln=q->col;
return(1);
}
q=q->right;
}
p=p->tag.next;
}
return (0);
}
main()
{
struct matnode *hm,*hm1,*hm2;
struct matnode *h[max],*h1[max];
int ci,cj,x;
char e;
printf("第一个距阵:\n");
hm1=createmat(h);//创建十字链表hm1
printf("第二个距阵:\n");
hm2=createmat(h1);//创建十字链表hm2
hm=addmat(hm1,hm2,h);
prmat(hm); //输出十字链表
printf("是否想查找元素:\n");
getchar();
scanf("%c",&e);
if(e=='y'||e=='Y')
{
printf("查找值:");
scanf("%d",&x);
if(findmat(hm,x,&ci,&cj))
printf("%d在第%d行第%d列\n",x,ci,cj);
else printf("未找到\n");
}
else printf("Time is over.Thank you!\n");
}