スパース行列を格納するためのクロスリンクリストメソッド



Cross Linked List Method Store Sparse Matrix



#include #include #define MAXSIZE 100 //Cross linked list method to store sparse matrix //Ordinary node typedef struct OLNode{ int col,row float value struct OLNode *right,*down }OLNode //Total header node-storage: number of rows, number of columns, non-zero element number (k), pointers to the right and lower base addresses typedef struct CrossList{ int m,n,k OLNode *rhead,*chead }CrossList int createCrossListMat(float A[][MAXSIZE],int m,int n,int k,CrossList &M){ //Null pointer--may be the second call to this function, so make it null if(M.rhead) free(M.rhead) if(M.chead) free(M.chead) M.k=k M.m=m M.n=n //Apply for base address space, return if there is no memory if(!(M.rhead=(OLNode*)malloc(n*sizeof(OLNode)))) return 0 if(!(M.chead=(OLNode*)malloc(n*sizeof(OLNode)))) return 0 //The base address is blank for(int i=0i<n++i){ M.rhead[i].down=NULL M.rhead[i].right=NULL } for(int j=0j<m++j){ M.chead[j].down=NULL M.chead[j].right=NULL } //Define the auxiliary address, update it in real time, and point to the non-zero freshman node OLNode *temp_c[n] for(int i=0i<n++i){ temp_c[i] = &(M.rhead[i]) } //Start storage for(int i=0i<m++i){ //Define the auxiliary address, update it in real time, and point to the non-zero freshman node OLNode *r = &(M.chead[i]) for(int j=0j<n++j){ if(A[i][j]!=0){ OLNode *p=(OLNode*)malloc(sizeof(OLNode)) p->value=A[i][j] p->row=i p->col=j p->right=NULL p->down=NULL temp_c[j]->down=p temp_c[j]=p r->right=p r=p } } } return 1 } int main(){ return 0 }