/* 输入矩阵的非领元素建立十字链表并按行方式打印该十字链表的完整程序 */ struct matnode /* 十字链表结点的定义 */ { int row,col; struct matnode *right,*down; union { int val; struct matnode *next; }tag; }; struct matnode *createmat() { int m,n,t,s,i,r,c,v; struct matnode *h[100],*p,*q; /* h[]是十字链表每行的表头指针数组 */ printf("行数m,列数n,非零元素个数t:"); 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; 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; } h[s]->tag.next=h[0]; for(i=1;i<=t;i++) /* t为非零元素个数 */ { printf("t 第%d个元素(行号m,列号n,值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; } return(h[0]); }
|