2013-02-20 98 views
3

在C中用於稀疏動態矩陣的最適合的數據結構是什麼? 我知道耶魯格式,但它是靜態矩陣。 我需要能夠添加行列和值。 謝謝C中的動態大型稀疏矩陣的數據結構

+1

僅供參考,如有任何問題首先是「什麼是最好的......」問及SO有沒有那麼多的「如果」它將被關閉,因爲* *的時候它會條件被關閉。 – WhozCraig 2013-02-20 09:09:59

+0

只有給定矩陣大小的近似值以及要優化的操作,才能定義最佳數據結構。需要轉置的稀疏100x100矩陣?打一個數組並完成它。 – UmNyobe 2013-02-20 09:15:54

回答

0

散列表。

示例:密鑰可以是row<<16|col

+0

這對隨機訪問很有用(如果你想在地點設置/清除)。如果你想遍歷行/列,然後使用UmNyobe的答案。 – 2013-02-20 09:29:00

+0

當然,不僅適用於位置的「設置/清除」,還適用於特定位置的「獲取」。 – 2013-02-20 10:30:10

3

一般來說,一個鏈表的數組。如果大多數操作是基於行的,則每個列表代表一行,否則,每個列表代表一列。您可以get more info here

typedef struct matrix { 
    node** rowList;  // rowList is a pointer to the array of rows 
    node** columnList; // column list is a pointer to the array of columns. 
    int rows, columns; // store the number of rows and columns of the matrix 
} matrix 


typedef struct node { 
    int row, column, 
    double value; 
    struct node* rowPtr; 
    struct node* colPtr; 
} node;