title: Values not getting Stored in Linked List in Addition of Sparse matrix C Program
body: <p>I'm Trying to Add 2 sparse matrix using linked list.
  I'm accepting values for 2 matrixes
  Adding them and Storing them into a third matrix.
  but for some reason the values are not getting stored in linked list, there is some memory issue.</p>
  
  <p>I tried checking the conditions for adding node at end in a linked list.</p>
  
  <pre><code>//Suyash Ekhande's Approach to Sparse matrix Addition and Substraction.
  #include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  #include&lt;stdlib.h&gt;
  struct SparseMatrix
  {
      int row;
      int col;
      int val;
      struct SparseMatrix *next;
  };
  struct SparseMatrix *m1;
  struct SparseMatrix *m2;
  struct SparseMatrix *m3;
  
  void printM1M2()
  {
      struct SparseMatrix *mm1,*mm2;
      mm1=m1;
      mm2=m2;
  
      printf("Matrix1: \n");
      while(mm1-&gt;next!=NULL)
      {
          printf("Row: %d\tCol: %d\tVal: %d\n",mm1-&gt;row,mm1-&gt;col,mm1-&gt;val);
          mm1=mm1-&gt;next;
      }
      printf("Matrix2: \n");
      while(mm1-&gt;next!=NULL)
      {
          printf("Row: %d\tCol: %d\tVal: %d\n",mm2-&gt;row,mm2-&gt;col,mm2-&gt;val);
          mm2=mm2-&gt;next;
      }
  }
  
  void addMat1Values()
  {
      struct SparseMatrix *ptr,*tmp;
      int row,col,val;
      ptr = (struct SparseMatrix*)malloc(sizeof(struct SparseMatrix));
  
      if(m1 == NULL)
      {
          printf("\nEnter Row Column and Value\n");
          scanf("%d %d %d",&amp;row, &amp;col, &amp;val);
          ptr-&gt;row=row;
          ptr-&gt;col=col;
          ptr-&gt;val=val;
          ptr-&gt;next = NULL;
          m1 = ptr;
  
          printf("\n1st Node inserted");
      }
      else
      {
          tmp=m1;
          printf("\nEnter Row Column and Value\n");
              scanf("%d %d %d",&amp;row, &amp;col, &amp;val);
          while(tmp-&gt;next!=NULL)
          {
              tmp=tmp-&gt;next;
  
          }
              ptr-&gt;row=row;
              ptr-&gt;col=col;
              ptr-&gt;val=val;
              tmp-&gt;next=ptr;
              ptr-&gt;next=NULL;
  
      printf("\nNode inserted");
      }
  }
  
  void addMat2Values()
  {
      struct SparseMatrix *ptr,*tmp;
      int row,col,val;
      ptr = (struct SparseMatrix*)malloc(sizeof(struct SparseMatrix));
      if(m2 == NULL)
      {
          printf("\nEnter Row Column and Value\n");
          scanf("%d %d %d",&amp;row, &amp;col, &amp;val);
          ptr-&gt;row=row;
          ptr-&gt;col=col;
          ptr-&gt;val=val;
          ptr-&gt;next = NULL;
          m1 = ptr;
  
      }
      else
      {
          tmp=m1;
          while(tmp-&gt;next!=NULL)
          {
              tmp=tmp-&gt;next;
  
          }
  
          printf("\nEnter Row Column and Value\n");
              scanf("%d %d %d",&amp;row, &amp;col, &amp;val);
              ptr-&gt;row=row;
              ptr-&gt;col=col;
              ptr-&gt;val=val;
              tmp-&gt;next=ptr;
              ptr-&gt;next=NULL;
      printf("\nNode inserted");
      }
  }
  void copyElement(int r,int c,int v)
  {
      struct SparseMatrix *ptr,*temp;
      ptr = (struct SparseMatrix*)malloc(sizeof(struct SparseMatrix));
  
      ptr-&gt;row=r;
      ptr-&gt;col=c;
      ptr-&gt;val=v;
      if(m3 == NULL)
      {
          ptr -&gt; next = NULL;
          m3 = ptr;
          printf("\nNode inserted");
      }
      else
      {
          temp = m3;
          while (temp -&gt; next != NULL)
          {
              temp = temp -&gt; next;
          }
          temp-&gt;next = ptr;
          ptr-&gt;next = NULL;
          printf("\nNode inserted");
  
      }
      }
  
  
  
  void addTheMatrixFinally()
  {
      struct SparseMatrix *mat1;
      struct SparseMatrix *mat2;
      mat1=m1;
      mat2=m2;
  
      if(mat1 == NULL || mat2 == NULL)
      {
          printf("Matrices are Null!!");
      }
      while(mat1 !=NULL &amp;&amp; mat2!=NULL)
      {
          if(mat1-&gt;row == mat2-&gt;row &amp;&amp; mat1-&gt;col == mat2-&gt;col)
          {
              int row,col,val;
              row=mat1-&gt;row;
              col=mat1-&gt;col;
              val=mat1-&gt;val + mat2-&gt;val;
              copyElement(row,col,val);
                mat1=mat1-&gt;next;
              mat2=mat2-&gt;next;
          }
          else if(mat1-&gt;row &lt; mat2-&gt;row || mat1-&gt;col &lt; mat2-&gt;col)
          {//mat 1 ele is smaller
           //copy the element to final matrix
           copyElement(mat1-&gt;row,mat1-&gt;col,mat1-&gt;val);
           mat1=mat1-&gt;next;
           //go to next element
          }
          else if(mat1-&gt;row &gt; mat2-&gt;row || mat1-&gt;col &gt; mat2-&gt;col)
          {//mat 2 ele is smaller
              //copy the element to final matrix
           copyElement(mat1-&gt;row,mat1-&gt;col,mat1-&gt;val);
           mat2=mat2-&gt;next;
           //go to next element
          }
  
      }
  }
  
  void printSparseMatrix()
  {
      int r,c,v;
      struct SparseMatrix *mat;
      mat = m3;
  
      if(mat == NULL)
      {
      printf("\n result matrice are null");
      }
      else
      {
          while(mat-&gt;next!=NULL)
          {
              r=mat-&gt;row;
              c=mat-&gt;col;
              v=mat-&gt;val;
              printf("Row: %d\tCol: %d\tVal: %d",r,c,v);
              mat=mat-&gt;next;
          }
      }
  }
  
  int main()
  {
      int l,i, j;
      clrscr();
      printf("Enter Number of Elements: ");
      scanf("%d",&amp;l);
      printf("Enter Matrix 1 Elements");
      for(i=0;i&lt;l;i++)
      {
          addMat1Values();
      }
      printf("\nEnter Matrix 2 Elements");
      for(j=0;j&lt;l;j++)
      {
          addMat2Values();    
      }
      printM1M2();
      addTheMatrixFinally();
      printSparseMatrix();
  
      getch();
      return 0;
  }
  </code></pre>
  
  <p>The Expected output should be the 3rd Matrix shown in the form of linked list.
  But the Output prompts the Matrix 1 and 2 to be Null which is checked by an Condition.</p>