2014-07-07 123 views
1

我在我的程序中遇到了與我的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)'MatrixNode *新=頭;':類型是不同的。 2)'new = create_node(); new = new-> next_row;':不形成鏈接。 – BLUEPIXY

+0

您還沒有創建足夠的節點。在你的示例中,你將有三個矩陣節點,它們有指向下一行的指針和三個指向下一列的(不同的)矩陣節點。您應該有九個節點,每個節點有兩個鏈接,一個用於下一行,一個用於下一列。 – ianharris

+0

是的,高爾基有道理,但我該怎麼做呢?我只需在for循環中添加new = new-> next_col和new = new-> next_row? – user3699735

回答

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; 

變化既爲循環並詢問你是否需要更多的幫助

+0

是prev和tmp應該是一樣的嗎? – user3699735

0
  1. 查找範圍create_linked_list_matrix。不要使用「new」作爲變量名稱,因爲它是一個保留關鍵字。如果你仍然想這樣稱呼它,你總是可以將它命名爲「new_」,所以它不會與保留關鍵字衝突。
  2. 此外,由於您如何設置for循環,該功能將創建n + 1行和列。如果您希望循環執行n次,請嘗試for (int i = 0; i < 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; 
    } 
    
+0

那我該怎麼去展示呢?我會使用嵌套for循環或什麼 – user3699735

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]; 
} 
+0

謝謝你的作品,但任何人都可以解釋這一點?就像我們使用嵌套for循環,idk我吮吸編碼,感謝您的幫助,雖然 – user3699735

+0

@ user3699735我認爲這很容易做到這一點是爲了把一個鏈接在垂直和水平方向。 – BLUEPIXY

+0

我認爲它是通過改變處理的順序來總結循環,但很容易理解。 – BLUEPIXY