简体   繁体   中英

sparse matrix using linked list C++

I'm coding the program that using linked list to store a sparse matrix. First I create a class "Node" contains the index of entry, value of entry and two pointers to next row and next column. Second I find on Google that I need to create the class Matrix like this code below but I don't understand the meaning of Node **rowList and node** columnList . Why they use a pointer to a pointer there and how could I implement a matrix from that? Thank you so much.

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
}

It appears to be exactly what the comment says. They are arrays. Presumably rowList will be an array of rows elements, and columnList will be an array of cols elements. The reason it's a Node** is that each item in the array is a Node* . A pointer to an array always has an extra level of indirection (an extra * ). That means when you index a single element out of that array you get a value of type Node* again.

The arrays are created like this:

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;

When you need to delete them (in the destructor for Matrix ):

delete [] rowList;
delete [] colList;

As for your question on how to implement your matrix from that, that's really up to you. Presumably when you create a node at position (i, j) , you append that node to each of rowList and columnList . ie :

Node * node = new Node(i, j, 123.0);
rowList[i] = node;
columnList[j] = node;

But it's not that simple, because the node obviously must be linked into both a row and column list. At the very basic level, and using the structures you've provided, here's one way:

// 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()...

Now using the above with my original example:

Node * node = new Node(i, j, 123.0);
insert_row( rowList[i], node );
insert_col( columnList[j], node );

For ordered insert

Since you have code already, I will offer my take on it. But you still need to do some work yourself.

I just try to understand the concept but it's so confusing for me.

Let's just clean things up to begin with. It's a class, and you're using C++ so please use your C++ knowledge:

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]
}

So, now you need to implement the Matrix::inputData function... Essentially you do what your friend was trying to do, but without the errors and memory leaks. That means you start like this:

// 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]
}

Now, you finish the function off, and don't forget to do the column indexing...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM