2017-04-09 104 views
-1

老實說,當談到鏈表時,我不知道自己在做什麼。這裏的東西導致了分段錯誤,但我對鏈接列表知之甚少,不知道它在哪裏,更不用說,它們令人困惑。我有代碼進行調試,但在發佈之前將其取出。代碼似乎到了main中的for語句,我得到了錯誤。用戶輸入他們想要打印的節點數量以及許多打印的隨機數字,其中十個打印在每行上。鏈接列表出現分段錯誤

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

struct node_def 
{ 
    int data; 
    struct node_def *next; 
}; 
typedef struct node_def node; 

node *makeNode (int val); 
node *insertFront(node *head,node *new); 
void printList(node *head); 

int numNodes = 0; 

int main() 
{ 
    srand(time(0)); 

    int i = 0; 

    node *head = NULL; 
    node *new = NULL; 

    printf("How many nodes? ", numNodes); 
    scanf("%d", &numNodes); 
    printf("\n"); 

    head = insertFront(head, new); 
    for(i = 0; i < numNodes; ++i) 
    { 
     makeNode(numNodes); 
    /* printList(head);*/ 
     /*insertFront(head, new);*/ 
    } 
    printList(head); 

    printf("\n"); 
    return 0; 
} 
node *makeNode (int val) 
{ 

    node *head = NULL; 

    node *new = malloc(sizeof(node)); 
    new->data = rand() % 10000; 
    new->next = NULL; 
    if (head == NULL) 
     head = new; 



    return new; 
} 
node *insertFront(node *head, node *new) 
{ 

    new->next = head; 

    return head; 
} 
void printList(node *head) 
{ 
    int j = 0; 
    for(j = 0; j < numNodes; ++j) 
    { 
     while (head != NULL) 
     { 
      printf(" %4d", head->data); 
      head = head->next; 
     } 
     if(j % 10 == 0) 
      printf("\n"); 
    } 
    return; 
} 
+2

程序不能正常工作?非常具體的描述。你給什麼輸入?除了核心轉儲之外,你還能得到什麼輸出?調試器告訴你什麼?你添加了多少診斷打印,它告訴你什麼?你可以使用斷言? –

+0

@JonathanLeffler代碼似乎到了main中的for語句,然後我得到錯誤。用戶輸入他們想要打印的節點數量,並且用分配給它們的隨機數打印許多節點,其中十個打印在一行上。 – mychem97

+0

你用'new = NULL'調用'insertFront'。你應該學習如何使用調試器。 – Siguza

回答

1

該程序在這裏有多個問題。基本上你寫的代碼根本沒有實現鏈表。

  1. 此函數makenode不正確。

    node *makeNode (int val) 
    { 
    
        node *head = NULL; 
    
        node *new = malloc(sizeof(node)); 
        new->data = rand() % 10000; 
        new->next = NULL; 
        if (head == NULL) 
         head = new; 
    
        return new; 
    } 
    

    在這段代碼中,head被初始化爲NULL。所以如果(head == NULL)將始終爲真。爲什麼val參數在不使用時需要makeNode?您可以更新到下面的代碼:

    我的建議:

    node *makeNode() 
    { 
        node *new = malloc(sizeof(node)); 
        new->data = rand() % 10000; 
        new->next = NULL; 
        return new; 
    } 
    

    你不必在這裏檢查頭= NULL。

  2. 你正在做NULL指針解引用(這就是爲什麼你有段錯誤)。

    node *head = NULL; 
    node *new = NULL; 
    
    head = insertFront(head, new); 
    

    這裏頭和新都是NULL。現在在insertFrontnew->next將segfault。

    的參數insertFront,尚未初始化。你不認爲在插入它之前你應該先爲腦袋分配內存嗎?

    我的建議:(主函數)

    node *head = makeNode(); 
    for(i = 0; i < numNodes; ++i) 
    { 
        node *new = makeNode(); 
        head = insertFront(head, new); 
    } 
    
  3. 再次的printList功能也是不正確的。我建議你看一遍並進行調試。我不想逐行檢查它。

    但在這裏,你應該是什麼真正做:

    void printList(node *head) 
    { 
        if (head == NULL) return; 
    
        node *temp = head; 
    
        while (temp != NULL) 
        { 
         printf(" %d\n", temp->data); 
         temp = temp->next; 
        } 
    } 
    

    就這麼簡單。我建議你仔細看看代碼,並嘗試一步一步地進行調試。

還要確保在將節點插入列表之前,爲節點分配內存。去筆和紙的方式,跟蹤你的代碼一步一步。這就是我用鏈接列表工作的方式。

+0

不錯的分析。你沒有問爲什麼'val'被傳遞給'makeNode()',但被忽略 - 使用隨機數作爲值,而不是傳遞給函數的任何值。你也調用你的'makeNode()',就好像它是一個不帶參數的函數一樣。 –

+0

是的。你在那裏得到了一個有效的點。我想我沒有注意到makeNode正在採取val參數:)更新我的代碼。 – paratrooper

+0

'printList()'中的'if'測試是一個非常小的優化。你完全可以忽略它,這個函數也可以工作。 –