【问题标题】:Creating a Matrix using a linked list使用链表创建矩阵
【发布时间】: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-&gt;next_row;:不形成链接。
  • 您也没有创建足够的节点。在您的示例示例中,您将拥有三个具有指向下一行的指针的矩阵节点和三个(不同的)指向下一列的矩阵节点。您应该有九个节点,每个节点都有两个链接,一个用于下一行,一个用于下一列。
  • 是的,高尔基体说得通,但我该怎么做呢?我可以在我的 for 循环中添加一个 new = new->next_col 和 new = new->next_row 吗?
  • 如果有人可以帮助我,仍然需要一些帮助来显示这个
  • 您需要使用不同的变量行和列。

标签: c memory-management matrix linked-list


【解决方案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];
}

【讨论】:

  • 谢谢你的作品,但谁能解释一下?就像我们使用嵌套的 for 循环一样,我不擅长编码,不过感谢您的帮助
  • @user3699735 我认为这样做比较容易,这样可以在垂直和水平方向放置链接。
  • 我认为是通过改变处理的顺序来总结循环,但容易理解。
【解决方案2】:

好的,让我们仔细看看你的函数。

假设您使用 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 循环并询问您是否需要更多帮助

【讨论】:

  • prev 和 tmp 应该是一样的吗?
【解决方案3】:
  1. 查看 create_linked_list_matrix。不要使用“new”作为变量名,因为它是保留关键字。如果您仍然想这样称呼它,您可以随时将其命名为“new_”,这样它就不会与保留关键字冲突。
  2. 此外,由于您设置 for 循环的方式,同一函数将创建 n+1 行和列。如果您希望循环执行 n 次,请尝试 for (int i = 0; i &lt; n; i++)
  3. 在同一个函数中,您没有正确链接您的链表。首先,尝试为行和列嵌套 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;
    }
    

【讨论】:

  • 那么我将如何显示这个?我会使用嵌套的 for 循环还是什么
猜你喜欢
  • 2020-08-08
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2020-11-24
相关资源
最近更新 更多