십자 링크 저장 희소 행렬

8568 단어 데이터 구조
/*       

typedef struct GNode *GList;
struct GNode{
    int Tag; //   :0        ,1         
    union{  //     SubList       Data  ,        
        ElementTtype Data;
        GList SubList;
    }URegion;
    GList Next; //        
};
*/ 

#include 
#include 
#define MAX 100
/*
             
      :
        1.     head,         
         col:    ,row:    ,value:      
        2,           
        3.       :   ,  ,   
*/ 

/*     

A= 18 0 0
   0 27 0
   0 0 0
   23 -1 0 

*/ 

//    
typedef struct SparseMatrix{
    struct SparseMatrix *Down;
    struct SparseMatrix *Right;
    union{
        struct{
            int row;
            int col;
            int value;
        };
        struct SparseMatrix *Next;
    }URegion;
}SparseMatrix; 

//         
void smInitialize(SparseMatrix *smArray[],int row,int col);
//        ,     
void smInsertTerm(SparseMatrix *mySM[],int row,int col,int value);
//    ,        
void smPrint(SparseMatrix *mySM);
//             ,    1,    0
int smFindTerm(SparseMatrix *mySm[],int row,int col);
//           ,    1,    0
int smDeleteTerm(SparseMatrix *mySM[],int row,int col);

int main(int argc, char *argv[])   
{  
    SparseMatrix *term00[MAX]={NULL};  
    smInitialize(term00,4,3);  

    smInsertTerm(term00,1,1,18);  
    smInsertTerm(term00,2,2,27);  
    smInsertTerm(term00,4,3,24);  
    smInsertTerm(term00,4,1,23);  
    smInsertTerm(term00,4,2,-1);  
    smInsertTerm(term00,1,2,20 ); 
    smPrint(term00[0]);

    return 0;  
} 

void smInitialize(SparseMatrix *smArray[],int row,int col)  
{  
    SparseMatrix *term00=(SparseMatrix*)malloc(sizeof(SparseMatrix));  
    term00->URegion.row=row;  
    term00->URegion.col=col;  
    term00->URegion.value=0;  
    term00->Right=term00;  
    term00->Right=NULL;  
    smArray[0]=term00;  
    //   head     
    int headNum=row>col?row:col;  
//  if(headNum>MAX)  
//     throw;  
    for(int i=1;i<=headNum;i++){  
        smArray[i]=(SparseMatrix*)malloc(sizeof(SparseMatrix));;  
        if(i==1){  
            term00->Right=smArray[i];  
        }else{  
            smArray[i-1]->URegion.Next=smArray[i];  
        }  
        smArray[i]->Down=smArray[i];//          
        smArray[i]->Right=smArray[i];//          
    }  

    smArray[headNum]->URegion.Next=smArray[0];//head term00         
}  
void smInsertTerm(SparseMatrix* mySM[],int row,int col,int value)  
{  
//  if(mySM[0]->URegion.rowURegion.col
//      throw ;  
    //     , ,    
    int repeatFlag=0;  
    SparseMatrix* term=(SparseMatrix*)malloc(sizeof(SparseMatrix));  
    term->URegion.col=col;  
    term->URegion.row=row;  
    term->URegion.value=value;   
    //             
    SparseMatrix* rowP=mySM[row]->Right;  
    SparseMatrix* rowLastP=mySM[row];  
    SparseMatrix* colP=mySM[col]->Down;  
    SparseMatrix* colLastP=mySM[col];  
    while(rowP!=mySM[row] && rowP->URegion.col<=col){  
        rowLastP=rowP;  
        if(rowP->URegion.col!=col){  
            rowP=rowP->Right;  
        }else{  
            repeatFlag=1;  
        }  
    }   

    //      ,           
    if(repeatFlag){  
        term->Right=rowP->Right;  
    }else{  
        term->Right=rowP;  
    }  
    rowLastP->Right=term;  


    while(colP!=mySM[col]  && colP->URegion.rowDown;  
    }  
    //      ,            
    if(repeatFlag){  
        term->Down=colP->Down;  
    }else{  
        term->Down=colP;  
    }  
    colLastP->Down=term;  
    //        
    if(repeatFlag)   
    {  
        free(colP);  
    }  

    mySM[0]->URegion.value+=1;   
}  

void smPrint(SparseMatrix* mySM){  
    SparseMatrix *head;  
    if(mySM){  
        head=mySM->Right;  
    }else{  
        return;  
    }  
    SparseMatrix *colP=head->Right;  
    SparseMatrix* headP=head;  

    while(headP!=mySM){  
        while(colP!=headP){  
            printf("%d,%d,%d||%d
"
,colP->URegion.value,colP->URegion.row,colP->URegion.col,mySM->URegion.value); colP=colP->Right; } headP=headP->URegion.Next; colP=headP->Right; } }

좋은 웹페이지 즐겨찾기