[英]sparse matrix using linked list C++
我正在編寫使用鏈表來存儲稀疏矩陣的程序。 首先我創建一個類“Node”包含條目索引,條目值和下一行和下一列的兩個指針。 其次我在Google上發現我需要像下面的代碼一樣創建類Matrix,但我不理解Node **rowList
和node** columnList
。 為什么他們使用指向那里的指針,我怎么能從那里實現矩陣? 非常感謝。
class Node
{
public:
int iValue, jValue;
float value;
Node *rowPtr;
Node *colPtr;
};
class Matrix
{
Node **rowList; // rowList is the pointer to the array of rows
Node **columnList; // columnList is the pointer to the array of columns
int rows, cols; // number of rows and columns
}
它似乎正是評論所說的。 它們是數組。 推測rowList
將是一個rows
元素數組,而columnList
將是一個cols
元素數組。 它是Node**
的原因是數組中的每個項目都是Node*
。 指向數組的指針總是具有額外的間接級別(額外的*
)。 這意味着當您從該數組中索引單個元素時,您將再次獲得Node*
類型的值。
數組的創建方式如下:
rowList = new Node* [rows];
columnList = new Node* [cols];
// Don't forget to initialise the values to NULL! Here's the dull way:
for( int i = 0; i < rows; i++ ) rowList[i] = NULL;
for( int i = 0; i < cols; i++ ) columnList[i] = NULL;
當你需要刪除它們時(在Matrix
的析構函數中):
delete [] rowList;
delete [] colList;
關於如何從中實現矩陣的問題,這完全取決於您。 假設在位置(i, j)
創建節點時,將該節點附加到rowList
和columnList
每一個。 即 :
Node * node = new Node(i, j, 123.0);
rowList[i] = node;
columnList[j] = node;
但事情並非如此簡單,因為節點顯然必須鏈接到行列列表中。 在最基本的層面,並使用您提供的結構,這是一種方式:
// Inserts newNode at the head of the list and modifies the head pointer.
void insert_row( Node* & r, Node *newNode )
{
newNode->rowPtr = r;
if( r != NULL ) r->rowPtr = newNode;
r = newNode;
}
// Similarly with insert_col()...
現在使用上面的原始示例:
Node * node = new Node(i, j, 123.0);
insert_row( rowList[i], node );
insert_col( columnList[j], node );
既然你已有代碼,我會提供我的服務。 但是你仍然需要自己做一些工作。
我只是試着理解這個概念,但這讓我很困惑。
讓我們開始清理一下。 這是一個類,你正在使用C ++,所以請使用你的C ++知識:
class Node
{
public:
Node( int i, int j, int val );
void InsertRowAfter( Node* node );
void InsertColAfter( Node* node );
int iValue, jValue; // Row and column index, 1-based
float value; // Element value
Node *rowPtr; // Next element in this row (sorted by jValue)
Node *colPtr; // Next element in this column (sorted by iValue)
};
Node::Node( int i, int j, int val )
: iValue(i)
, jValue(j)
, value(val)
, rowPtr(NULL)
, colPtr(NULL)
{}
// Inserts the given node to follow this node in the row list
void Node::InsertRowAfter( Node* node )
{
// [go on, you do it]
}
// Inserts the given node to follow this node in the column list
void Node::InsertColAfter( Node* node );
{
// [go on, you do it]
}
所以,現在你需要實現Matrix::inputData
函數......基本上你做了你的朋友試圖做的事情,但沒有錯誤和內存泄漏。 這意味着你就像這樣開始:
// Use 'horz' and 'vert' to search through the row and column lists. If a
// node needs to be created, it will be stored in 'node'.
Node *horz = rowList[iValue - 1];
Node *vert = columnList[jValue - 1];
Node *node;
// If there is no row list or smallest jValue, insert at the head.
// Otherwise, search for an insert point.
if( !horz || horz->jValue > jValue )
{
// [go on, you do it]
}
else
{
// Move 'horz' pointer to position at which we will append a node.
Node *next = horz->rowPtr;
while( next && next->jValue <= jValue ) {
horz = next;
next = next->rowPtr;
}
// If replacing an existing value, there's nothing else to do.
if( horz->jValue == jValue ) {
horz->value = value;
return;
}
// Otherwise append a new node.
// [go on, you do it]
}
現在,您關閉了該功能,並且不要忘記進行列索引...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.