1. 程式人生 > >稀疏矩陣的十字連結串列

稀疏矩陣的十字連結串列

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{int hang,lie,zhi;
struct node *xia,*you;
}node,* link; //非零元素
typedef struct node1
{ node **hangtou,**lietou;
int hangshu,lieshu ,geshu;
}node1;//十字連結串列
node1 *shizijianli()
{node1 *s;
node *p,*v,*q;
int i,j,n,e=3;
s=(node1*)malloc(sizeof(node1));
printf("請輸入行數、列數和非零元素個數");
scanf("%d%d%d",&s->hangshu,&s->lieshu,&s->geshu);
s->hangtou=(link*)malloc((s->hangshu+1)*sizeof(link));
s->lietou=(link*)malloc((s->lieshu+1)*sizeof(link));
for(e=0;e<s->hangshu+1;e++)
s->hangtou[e]=0;
for(e=0;e<s->lieshu+1;e++)
s->lietou[e]=0;
printf("請輸入非零元素所在行列以及元素本身,以輸入輸入0行為標誌結束");
scanf("%d%d%d",&i,&j,&n);
while(i!=0)
{p=(node*)malloc(sizeof(node));
p->hang=i;
p->lie=j;
p->zhi=n;
q=0;
if(s->hangtou[i]==0)
{p->you=0;
s->hangtou[i]=p;
}
else
{v=s->hangtou[i];
while(v->you!=0&&j>v->lie)
{q=v;
v=v->you;}
if(j<v->lie&&q==0)
{s->hangtou[i]=p;
p->you=v;
}
if(j<v->lie&&q!=0)
{
p->you=v;
q->you=p;}
if(j>v->lie)
{p->you=v->you;
v->you=p;
}
}
q=0;
if(s->lietou[j]==0)
{p->xia=0;
s->lietou[j]=p;}
else
{v=s->lietou[j];
while(v->xia!=0&&v->hang<i)
{q=v;
v=v->xia;}
if(i<v->hang&&q==0)
{s->lietou[j]=p;
p->xia=v;
}
if(i<v->hang&&q!=0)
{
p->xia=v;
q->xia=p;}
if(i>v->hang)
{p->xia=v->xia;
v->xia=p;
}
}scanf("%d%d%d",&i,&j,&n);
}

return s;
}
int main()
{node1   *s;
s=shizijianli();
}
在這裡插入程式碼片