Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It appears to be exactly what the comment says. They are arrays. Presumably <code>rowList</code> will be an array of <code>rows</code> elements, and <code>columnList</code> will be an array of <code>cols</code> elements. The reason it's a <code>Node**</code> is that each item in the array is a <code>Node*</code>. A pointer to an array always has an extra level of indirection (an extra <code>*</code>). That means when you index a single element out of that array you get a value of type <code>Node*</code> again.</p> <p>The arrays are created like this:</p> <pre><code>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 &lt; rows; i++ ) rowList[i] = NULL; for( int i = 0; i &lt; cols; i++ ) columnList[i] = NULL; </code></pre> <p>When you need to delete them (in the destructor for <code>Matrix</code>):</p> <pre><code>delete [] rowList; delete [] colList; </code></pre> <p>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 <code>(i, j)</code>, you append that node to each of <code>rowList</code> and <code>columnList</code>. <em>ie</em>:</p> <pre><code>Node * node = new Node(i, j, 123.0); rowList[i] = node; columnList[j] = node; </code></pre> <p>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:</p> <pre><code>// Inserts newNode at the head of the list and modifies the head pointer. void insert_row( Node* &amp; r, Node *newNode ) { newNode-&gt;rowPtr = r; if( r != NULL ) r-&gt;rowPtr = newNode; r = newNode; } // Similarly with insert_col()... </code></pre> <p>Now using the above with my original example:</p> <pre><code>Node * node = new Node(i, j, 123.0); insert_row( rowList[i], node ); insert_col( columnList[j], node ); </code></pre> <hr> <h2>For ordered insert</h2> <p>Since you have code already, I will offer my take on it. But you still need to do some work yourself.</p> <blockquote> <p>I just try to understand the concept but it's so confusing for me.</p> </blockquote> <p>Let's just clean things up to begin with. It's a class, and you're using C++ so please use your C++ knowledge:</p> <pre><code>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] } </code></pre> <p>So, now you need to implement the <code>Matrix::inputData</code> function... Essentially you do what your friend was trying to do, but without the errors and memory leaks. That means you start like this:</p> <pre><code>// 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-&gt;jValue &gt; jValue ) { // [go on, you do it] } else { // Move 'horz' pointer to position at which we will append a node. Node *next = horz-&gt;rowPtr; while( next &amp;&amp; next-&gt;jValue &lt;= jValue ) { horz = next; next = next-&gt;rowPtr; } // If replacing an existing value, there's nothing else to do. if( horz-&gt;jValue == jValue ) { horz-&gt;value = value; return; } // Otherwise append a new node. // [go on, you do it] } </code></pre> <p>Now, you finish the function off, and don't forget to do the column indexing...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload