将值插入到链接列表的一系列指针中,数组在struct | c
我一直在尝试找出7-8H的问题,
我必须使用给定的结构和一系列指示器来构建矩阵乘法函数,以链接列表(矩阵)
所有内容必须由malloc分配,
这是给定的结构:
typedef struct cellNode
{
int cell;
struct cellNode* next;
} Node;
typedef struct matrix
{
int numRows;
int numColumns;
Node** rows;
} Matrix;
这是构建矩阵并分配矩阵的功能:
Matrix* MatrixBuilder(Matrix* A, int rows, int cols)
{
int i=0;
A->numColumns=cols;
A->numRows=rows;
A->rows = (Node**)malloc(rows * sizeof(Node*));
if (A->rows == NULL)
{
printf("memory error!");
exit(1);
}
for (i=0; i<rows; i++)
{
A->rows[i] = (Node*)malloc(rows * sizeof(Node));
if (A->rows[i] == NULL)
{
printf("memory error!");
exit(1);
}
}
for (i=0; i<rows; i++)
{
A->rows[i] = NULL;
}
return A;
}
这是使用给定数据构建节点的功能,并将其链接到列表:
Matrix* MatrixBuilder(Matrix* A, int rows, int cols)
{
int i=0;
A->numColumns=cols;
A->numRows=rows;
A->rows = (Node**)malloc(rows * sizeof(Node*));
if (A->rows == NULL)
{
printf("memory error!");
exit(1);
}
for (i=0; i<rows; i++)
{
A->rows[i] = (Node*)malloc(rows * sizeof(Node));
if (A->rows[i] == NULL)
{
printf("memory error!");
exit(1);
}
}
for (i=0; i<rows; i++)
{
A->rows[i] = NULL;
}
return A;
}
这是我的主要功能:
int main()
{
int n,m,k,i,x,j=0; /* x to help me insert integers into the list */
Matrix* A = (Matrix*)malloc(sizeof(Matrix));
Matrix* B = (Matrix*)malloc(sizeof(Matrix));
Matrix* C = (Matrix*)malloc(sizeof(Matrix));
printf("please enter n:");
scanf("%d",&n);
printf("please enter m:");
scanf("%d",&m);
printf("please enter k:");
scanf("%d",&k);
A = MatrixBuilder(A, n, m);
B = MatrixBuilder(B, m, k);
C = MatrixBuilder(C, n, k);
for (i=0; i<n; i++)
{
printf("Enter row %d data\n",i);
for (j=0; j<m; j++)
{
scanf("%d", &x);
NodeBuilder_Insert(A->rows[j], x);
}
}
return 0;
}
当我运行它时,当我在第一行中输入第一个元素时,它停止了,所以我解决了问题在节点构建器函数中,但是我找不到它, 而且我不确定我是否正确分配了所有内容...
i have been trying to figure out the problem for 7-8h,
the thing that i must build a matrix multiplication func, using a given structs and an array of pointers to linked lists(The Matrix)
everything must be allocated by malloc,
Here is the given structs:
typedef struct cellNode
{
int cell;
struct cellNode* next;
} Node;
typedef struct matrix
{
int numRows;
int numColumns;
Node** rows;
} Matrix;
and here is the function to build the matrix and to allocate it:
Matrix* MatrixBuilder(Matrix* A, int rows, int cols)
{
int i=0;
A->numColumns=cols;
A->numRows=rows;
A->rows = (Node**)malloc(rows * sizeof(Node*));
if (A->rows == NULL)
{
printf("memory error!");
exit(1);
}
for (i=0; i<rows; i++)
{
A->rows[i] = (Node*)malloc(rows * sizeof(Node));
if (A->rows[i] == NULL)
{
printf("memory error!");
exit(1);
}
}
for (i=0; i<rows; i++)
{
A->rows[i] = NULL;
}
return A;
}
and here is the function to build the node, with a given data and link it to the list:
Matrix* MatrixBuilder(Matrix* A, int rows, int cols)
{
int i=0;
A->numColumns=cols;
A->numRows=rows;
A->rows = (Node**)malloc(rows * sizeof(Node*));
if (A->rows == NULL)
{
printf("memory error!");
exit(1);
}
for (i=0; i<rows; i++)
{
A->rows[i] = (Node*)malloc(rows * sizeof(Node));
if (A->rows[i] == NULL)
{
printf("memory error!");
exit(1);
}
}
for (i=0; i<rows; i++)
{
A->rows[i] = NULL;
}
return A;
}
and this is my main function:
int main()
{
int n,m,k,i,x,j=0; /* x to help me insert integers into the list */
Matrix* A = (Matrix*)malloc(sizeof(Matrix));
Matrix* B = (Matrix*)malloc(sizeof(Matrix));
Matrix* C = (Matrix*)malloc(sizeof(Matrix));
printf("please enter n:");
scanf("%d",&n);
printf("please enter m:");
scanf("%d",&m);
printf("please enter k:");
scanf("%d",&k);
A = MatrixBuilder(A, n, m);
B = MatrixBuilder(B, m, k);
C = MatrixBuilder(C, n, k);
for (i=0; i<n; i++)
{
printf("Enter row %d data\n",i);
for (j=0; j<m; j++)
{
scanf("%d", &x);
NodeBuilder_Insert(A->rows[j], x);
}
}
return 0;
}
when i run it, it stops right when i enter the first element in first row, so i quess the problem is in the NodeBuilder function, but i cant find it,
and i am not sure if i allocated everything properly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论