# 十字链表的创建

#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef int Status;

typedef struct OLNode{
int col, row;
ElemType value;
struct OLNode *down, *right;

typedef struct{
int mu, nu, tu;
}CrossList;

Status CreateSMatrix_OL(CrossList *M){//创建稀疏矩阵M， 采用十字链表存储
int m, n, t;
int i, j, v;
if(M){
free(M);
}
scanf("%d%d%d", &m, &n, &t);
M -> mu = m;
M -> nu = n;
M -> tu = t;
M -> rhead = (OLink *)malloc((m + 1) * sizeof(OLink));
M -> chead = (OLink *)malloc((n + 1) * sizeof(OLink));
if(! M -> rhead){
exit(0);
}
if(! M -> chead){
exit(0);
}
M -> rhead = M -> chead = NULL;//初始化行列头指针向量，各行列链表为空
for(scanf("%d%d%d", &i, &j, &v); i != 0;scanf("%d%d%d", &i, &j, &v)){
p = (OLink )malloc((m + 1) * sizeof(OLink));
if(!p){
exit(0);
}
p -> col = i;
p -> row = j;
p -> value = v;
if(M -> rhead[i] == NULL || M -> rhead[i] -> row > j){
p -> right = M -> rhead[i];
M -> rhead[i] = p;
}
else{//寻找在行表中的插入位置
for(q = M -> rhead[i]; (q -> right) && q -> right -> col < j; q = q -> right);
p -> right = q -> right;
q -> right = p;
}//完成行插入
if(M -> chead[j] == NULL || M -> chead[j] -> row > i){
p -> down = M -> chead[j];
M -> chead[j] = p;
}
else{//寻找在列表中的插入位置
for(q = M -> chead[j]; (q -> down) && q -> down -> row < i; q = q -> down);
p -> down = q -> down;
q -> down = p;
}
}
return 1;
}