title: Sparse matrix insert in c
body: <p>I am trying to do an efficient sparse matrix multiplication. Right now I am reading the data into memory and this is how my data structure looks like: </p>
  
  <pre><code>typedef struct node{
  int x;
  int y;
  int value;
  struct node* row;
  struct node* col;
  }node;
  
  typedef struct matrix{
  int height;
  int width; 
  node** rowList;
  node** colList;
  }matrix;
  </code></pre>
  
  <p>My current code for the insertion is:</p>
  
  <pre><code>void insert(matrix** M, int row_index, int col_index, int value)
  {
      node* currNode=(node*)malloc(sizeof(node));
      currNode-&gt;x=row_index;
      currNode-&gt;y=col_index;
      currNode-&gt;value=value;
  
      if ((*M)-&gt;rowList[row_index] == NULL) { /* index is empty */
          currNode-&gt;row = NULL;
          (*M)-&gt;rowList[row_index] = currNode;
      }
      else if ((*M)-&gt;rowList[row_index]-&gt;y &gt; col_index) { /* insert node to front */
          //printf("%d, %d\n", (*M)-&gt;rowList[row_index]-&gt;y, col_index);
          currNode-&gt;col = (*M)-&gt;rowList[row_index];
          (*M)-&gt;rowList[row_index] = currNode;
      }
      else if ((*M)-&gt;rowList[row_index]-&gt;y &lt; col_index) { /* insert node to front */  
          node* rowptr = (node*)malloc(sizeof(node));
          rowptr = (*M)-&gt;rowList[row_index];
          while(rowptr-&gt;col!=NULL&amp;&amp;rowptr-&gt;col-&gt;y &lt; col_index)
              rowptr=rowptr-&gt;col;
  
          currNode-&gt;col=rowptr-&gt;col;
          rowptr-&gt;col=currNode;
          //printf("-----------------%d\n", rowptr-&gt;y);
      }
  
      if ((*M)-&gt;colList[col_index] == NULL) { 
          currNode-&gt;col = NULL;
          (*M)-&gt;colList[col_index] = currNode;
      }
      else
      if ((*M)-&gt;colList[col_index]-&gt;x &gt; row_index) { 
          //printf("%d, %d\n", (*M)-&gt;colList[col_index]-&gt;x, row_index);
          currNode-&gt;row = (*M)-&gt;colList[col_index];
          (*M)-&gt;colList[col_index] = currNode;
      }
  }
  </code></pre>
  
  <p>In case of you ask, this is my print function:</p>
  
  <pre><code>void print_matrix(matrix *M){
      for(int i=0;i&lt;M-&gt;height;i++){
          while(M-&gt;rowList[i]!=NULL){
              printf("i=%d, j=%d, v=%d\n",M-&gt;rowList[i]-&gt;x, M-&gt;rowList[i]-&gt;y,
                     M-&gt;rowList[i]-&gt;value);
              M-&gt;rowList[i]=M-&gt;rowList[i]-&gt;col;
          }
      }
  }
  </code></pre>
  
  <p>For this input:</p>
  
  <pre><code>5,5
  0,0,1
  0,1,2
  0,3,3
  0,4,4
  </code></pre>
  
  <p>where (5,5) matrix dimensions and (0,0,1) = i,j,value, I get this:</p>
  
  <pre><code>i=0, j=0, v=1
  i=0, j=1, v=2
  i=0, j=3, v=3
  i=0, j=4, v=4
  i=0, j=4, v=4
  </code></pre>
  
  <p>For this input:</p>
  
  <pre><code>5,5
  0,0,1
  0,1,2
  0,3,3
  0,4,4
  0,2,5
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>i=0, j=0, v=1
  i=0, j=1, v=2
  i=0, j=2, v=5
  i=0, j=2, v=5
  </code></pre>
  
  <p>I think the problem is here:</p>
  
  <pre><code>else if ((*M)-&gt;rowList[row_index]-&gt;y &lt; col_index) {
      node* rowptr = (node*)malloc(sizeof(node));
      rowptr = (*M)-&gt;rowList[row_index];
      while(rowptr-&gt;col!=NULL&amp;&amp;rowptr-&gt;col-&gt;y &lt; col_index)
          rowptr=rowptr-&gt;col;
  
          currNode-&gt;col=rowptr-&gt;col;
          rowptr-&gt;col=currNode;
      }
      [ ... ]
  </code></pre>
  
  <p>Somehow I remove one of the values when I add a new element that is smaller.</p>
  
  <p>The question is : how can I get this code to load my sparse matrix values into memory using the data structure provided correctly?</p>
  
  <p>Thank you ^^</p>