2013-02-20 94 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;