0

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
}

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

5 Comments

I just update my code and question. Could you please answer that?
In my example I just inserted the value, but I did mention that it's very basic. Your friend is doing an ordered insert - ie Searching through the list so that the row (and column) indices are kept in order. His code is confusing because it is almost completely uncommented, and the comments it does have are unhelpful. Don't copy your friend's code. He's doing at least one stupid thing, and a bunch of redundant things. Just work it out on your own.
I think he does a lot of redundant things, too. I don't copy his code, I just try to understand the concept but it's so confusing for me. I spend ~72 hours on this and still can't work it out. I try to run my friend code and it works, but if you can show a shorter way to do that, that would be great and I really appreciate that.
Have edited. In this case I gave you some code. I think that if this is still confusing then you need to draw pictures of your lists to visualise what you are trying to do.
I can't express my gratitude for you taking the time and thought you put into your post. Thank you for the information you added and it has really sent me off in the right direction! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.