【发布时间】:2019-10-21 08:18:25
【问题描述】:
我正在尝试使用链表添加 2 个稀疏矩阵。 我接受 2 个矩阵的值 添加它们并将它们存储到第三个矩阵中。 但由于某种原因,这些值没有存储在链表中,存在一些内存问题。
我尝试检查在链表末尾添加节点的条件。
//Suyash Ekhande's Approach to Sparse matrix Addition and Substraction.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
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->next!=NULL)
{
printf("Row: %d\tCol: %d\tVal: %d\n",mm1->row,mm1->col,mm1->val);
mm1=mm1->next;
}
printf("Matrix2: \n");
while(mm1->next!=NULL)
{
printf("Row: %d\tCol: %d\tVal: %d\n",mm2->row,mm2->col,mm2->val);
mm2=mm2->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",&row, &col, &val);
ptr->row=row;
ptr->col=col;
ptr->val=val;
ptr->next = NULL;
m1 = ptr;
printf("\n1st Node inserted");
}
else
{
tmp=m1;
printf("\nEnter Row Column and Value\n");
scanf("%d %d %d",&row, &col, &val);
while(tmp->next!=NULL)
{
tmp=tmp->next;
}
ptr->row=row;
ptr->col=col;
ptr->val=val;
tmp->next=ptr;
ptr->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",&row, &col, &val);
ptr->row=row;
ptr->col=col;
ptr->val=val;
ptr->next = NULL;
m1 = ptr;
}
else
{
tmp=m1;
while(tmp->next!=NULL)
{
tmp=tmp->next;
}
printf("\nEnter Row Column and Value\n");
scanf("%d %d %d",&row, &col, &val);
ptr->row=row;
ptr->col=col;
ptr->val=val;
tmp->next=ptr;
ptr->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->row=r;
ptr->col=c;
ptr->val=v;
if(m3 == NULL)
{
ptr -> next = NULL;
m3 = ptr;
printf("\nNode inserted");
}
else
{
temp = m3;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->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 && mat2!=NULL)
{
if(mat1->row == mat2->row && mat1->col == mat2->col)
{
int row,col,val;
row=mat1->row;
col=mat1->col;
val=mat1->val + mat2->val;
copyElement(row,col,val);
mat1=mat1->next;
mat2=mat2->next;
}
else if(mat1->row < mat2->row || mat1->col < mat2->col)
{//mat 1 ele is smaller
//copy the element to final matrix
copyElement(mat1->row,mat1->col,mat1->val);
mat1=mat1->next;
//go to next element
}
else if(mat1->row > mat2->row || mat1->col > mat2->col)
{//mat 2 ele is smaller
//copy the element to final matrix
copyElement(mat1->row,mat1->col,mat1->val);
mat2=mat2->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->next!=NULL)
{
r=mat->row;
c=mat->col;
v=mat->val;
printf("Row: %d\tCol: %d\tVal: %d",r,c,v);
mat=mat->next;
}
}
}
int main()
{
int l,i, j;
clrscr();
printf("Enter Number of Elements: ");
scanf("%d",&l);
printf("Enter Matrix 1 Elements");
for(i=0;i<l;i++)
{
addMat1Values();
}
printf("\nEnter Matrix 2 Elements");
for(j=0;j<l;j++)
{
addMat2Values();
}
printM1M2();
addTheMatrixFinally();
printSparseMatrix();
getch();
return 0;
}
预期的输出应该是以链表形式显示的第三个矩阵。 但输出提示矩阵 1 和 2 为 Null,由条件检查。
【问题讨论】:
标签: c data-structures linked-list