十字链表
定义:
typedef struct node
{int i,j;
struct node *down,*right;
union
{struct node *next;
datatype v;} val;
} linklist;
建表:
linklist *creatcrosslink()
{linklist *p,*q,*h,*cp[smax];
int i,j,m,n,t,s;
datatype v;
sacnf("%d%d%d",&m,&n,&t);
if(m>n) s=m;
else s=n;
h=malloc(sizeof(linklist));
h->i=m;h->j=m;
cp[0]=h;
for(i=1;i<=s;i++)
{p=malloc(sizeof(linklist));
p->i=0;p->j=0;
p->right=p;
p->down=p;
cp=p;
cp[i-1]->val.next=p;
}
cp->val.next=h;
for(i=1;i<=t;i++)
{scanf("%d%d%d",&i,&j,&v)
p=malloc(sizeof(linklist));
p->i=i;p->j=j;p->val.v=v;
q=cp;
while((q->right!=cp)&&(q->right->jright;
p->right=q->right;
q->right=p;
q=cp[j];
while((q->down!=cp[j])&&(q->down->idown;
p->down=q->down;
q->down=p;
}
return h;
}
一个插入函数:
insert(crow,ccol,cval,cp)
int crow,ccol,cval;
linklist *cp[smax];
{linklist *s,*last;
s=malloc(sizeof(linklist));
s->i=crow;s->j=ccol;s->v=cval;
last->right=s;last=s;
cp[ccol]->next->down=s;
cp[ccol]->next=s;
}
加法运算:
linklist *matrixadd(a,b,c,cp)
linklist *a,*b,*c,*cp[smax];
{int n,i,arow,brow;
linklist *last,*arow,*brow,*s,*pa,*pb;
c=malloc(sizeof(linklist));
c->i=a->i;c->j=a->j;
if(a->i>a->j) n=a->i;
else n=a->j;
cp[0]=c;
for(i=1;i<=n;i++)
{s=malloc(sizeof(linklist));
s->i=0;s->j=0;
s->right=s;s->down=s;
sp=s;
}
arow=a->next;brow=b->next;
last=cp[0];
i=0;
while((arow!=a)&&(brow!=b))
{pa=arow->right;pb=brow->right;
while((pa!=arow)&&(pb!=brow))
{
if(pa->j==pb->j)
{if(pa->v+pb->v!=0)
insert(pa->i,pa->j,pa->v+pb->v);
pa=pa->right;pb=pb->right;
}
else
if(pa->j>pb->j)
{insert(pb->i,pb->j,pb->v);
pb=pb->right;
}
else
{insert(pa->i,pa->j,pa->v);
pa=pa->right;
}
}
while(pa!=arow)
{insert(pa->i,pa->j,pa->v);
pa=pa->right;}
while(pb!=arow)
{insert(pb->i,pb->j,pb->v);
pb=pb->right;
}
last->right=cp;
i++;
last=cp;
arow=arow->next;brow=brow->next;
}
for(i=1;i<=c->j;i++)
cp->next->down=cp;
for(i=1;i<=n-1;i++)
cp->next=cp[i+1];
if(n==0) c->next=c;
else
{cp[n]->next=c;
c->next=cp[1];
}
} |