我在我的程序中遇到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
答案 0 :(得分:1)
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *mat[rows][cols];//use malloc if the number of elements is large
int r, c;
for(r = 0; r < rows; ++r){
for(c = 0; c < cols; ++c){
mat[r][c] = create_node();
}
}
for(r = 0; r < rows; ++r){
for(c = 0; c < cols; ++c){
if(c < cols -1)
mat[r][c]->next_col = mat[r][c+1];
if(r < rows -1)
mat[r][c]->next_row = mat[r+1][c];
}
}
*head = mat[0][0];
}
void display_matrix(MatrixNode *head){
MatrixNode *row = head;
while(row){
MatrixNode *col = row;
while(col){
printf("%d\t", col->value);
col = col->next_col;
}
printf("\n");
row = row->next_row;
}
}
简化版
void create_linked_list_matrix(MatrixNode **head, const int rows, const int cols){
MatrixNode *node, *mat[cols];
int r, c;
for(r = rows-1; r >= 0; --r){
for(c = cols-1; c >=0; --c){
node = create_node();
if(r < rows -1)
node->next_row = mat[c];
mat[c] = node;
if(c < cols -1)
mat[c]->next_col = mat[c+1];
}
}
*head = mat[0];
}
答案 1 :(得分:0)
好的,让我们仔细看看你的功能。
假设您使用create_node()函数创建一个新节点。
new = create_node() //You state that new points to that new node.
new = new->next_row; //You state that new points to NULL again
如您所见,一个节点与下一个节点之间没有连接。 要解决这个问题,我会使用帮助指针:
MatrixNode *new,*tmp = head;
new = create_node();
if (prev != NULL) {
prev->next = new;
prev = prev->next;
}
else //Means that this is the first element
prev=new;
更改两个for循环并询问您是否需要更多帮助
答案 2 :(得分:0)
for (int i = 0; i < n; i++)
。在同一个功能中,您没有正确链接链接列表。首先,尝试为行和列嵌套for循环。然后,在创建新节点后,请记住将节点链接到左侧(如果适用)及其上方的节点。您需要跟踪这些节点。跟踪上面第一列和第一行中的节点,以便在完成一行时指向上面的行。
MatrixNode *tmpabove = NULL, *tmpleft = NULL, *tmpaboveleft = NULL, *newnode = NULL;
for(i = 0; i < rows; i++){
tmpleft = NULL;
for(y = 0; y < cols; y++){
newnode = create_node();
if (tmpabove != NULL) tmpabove -> nextrow = newnode;
if (tmpleft != NULL) tmpleft -> nextcolumn = newnode;
else {
tmpaboveleft = newnode;
tmpleft = newnode;
}
tmpabove = tmpabove -> nextcolumn;
tmpleft = tmpleft -> nextcolumn;
}
tmpabove = tmpaboveleft;
}