【发布时间】:2014-07-07 14:18:04
【问题描述】:
我的程序中的display_matrix 函数出现问题,并且在我运行程序时也收到分段错误,我不知道这是否与我还没有destroy_memory 函数或什么有关.我的程序读取矩阵的维度,然后创建 1 到 100 的随机数并生成矩阵,然后显示它,然后释放分配的内存。我相信对于我的显示功能,我必须使用嵌套的 for 循环来打印出值。任何帮助表示赞赏,并提前感谢您。这是我现在拥有的代码以及我的程序的示例输出。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct MatrixNode_{
int value;
struct MatrixNode_ *next_row;
struct MatrixNode_ *next_col;
}MatrixNode;
void create_linked_list_matrix(MatrixNode** head, const int rows, const int cols);
MatrixNode* create_node(void);
void display_matrix(MatrixNode* head);
void destroy_linked_list_matrix(MatrixNode** head);
int main (int argc, char *argv[]) {
if (argc < 3) {
exit(1);
}
srand(time(NULL));
const int rows = atoi(argv[1]);
const int cols = atoi(argv[2]);
MatrixNode* head_a = NULL;
printf("Matrix A\n");
create_linked_list_matrix(&head_a,rows,cols);
display_matrix(head_a);
destroy_linked_list_matrix(&head_a);
}
MatrixNode* create_node(void){
MatrixNode *temp = (MatrixNode *)malloc(sizeof(MatrixNode));
if(temp == NULL){
printf("Memory alloc error");
exit(1);
}
temp->next_col = NULL;
temp->next_row = NULL;
temp->value = rand() % 100 + 1;
return temp;
}
/*The MatrixNode double pointer will be NULL when first coming in,
make sure to allocate space For it and adjust your linked list of linked
list to start from 1 not 0. Next allocate a linked list of
MatrixNodes using the next row as the next node in the linked list.
You will need to create the linked list length up to the passed in rows value.
After the allocation of the rows linked list, we need to allocate a separate
linked list for each of the next_col MatrixNode pointers in the rows linked list.
To create the linked list for the columns create the linked list of MatrixNodes
using the next_col as the next node in the linked list. You will need to create
the linked list length up to the passed in cols value.
Use the create_node function to create nodes for your linked list.
*/
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *tmpabove = NULL, *tmpleft = NULL, *tmpaboveleft = NULL, *newnode = NULL;
int i, y;
for( i = 0; i < rows; i++){
tmpleft = NULL;
for( y = 0; y < cols; y++){
newnode = create_node();
if(tmpabove != NULL) tmpabove->next_row = newnode;
if(tmpleft != NULL) tmpleft->next_col = newnode;
else{
tmpaboveleft = newnode;
tmpleft = newnode;
}
tmpabove = tmpabove->next_col;
tmpleft = tmpleft->next_col;
}
tmpabove = tmpaboveleft;
}}
void display_matrix(MatrixNode* head){
MatrixNode *temp = head;
while(temp != NULL){
printf("%d", temp->val);
temp = temp->next_col;
}
temp = temp->next_row;
}
样本输出:
./a.out 3 3
Matrix A
66 39 33
13 94 15
94 64 23
【问题讨论】:
-
1)
MatrixNode *new = head;:类型不同。 2)new = create_node(); new = new->next_row;:不形成链接。 -
您也没有创建足够的节点。在您的示例示例中,您将拥有三个具有指向下一行的指针的矩阵节点和三个(不同的)指向下一列的矩阵节点。您应该有九个节点,每个节点都有两个链接,一个用于下一行,一个用于下一列。
-
是的,高尔基体说得通,但我该怎么做呢?我可以在我的 for 循环中添加一个 new = new->next_col 和 new = new->next_row 吗?
-
如果有人可以帮助我,仍然需要一些帮助来显示这个
-
您需要使用不同的变量行和列。
标签: c memory-management matrix linked-list