2017-10-05 57 views
-1

我正在嘗試創建10個節點的列表並使用值1至10進行賦值並打印它們。我用下面的代碼嘗試了它,但是我以分段錯誤結束了。鏈接列表C程序中的分段錯誤

我是很新的鏈表中C.

#include<stdio.h> 
typedef struct Node 
{ 
    int data; 
    struct Node *next; 
}Node_Struct; 

int main(void) 
{ 
int i =0; 
Node_Struct* Node = NULL; 
Node = (Node_Struct*)malloc(sizeof(Node_Struct)); 

for (i = 1; i<=10; i++){ 
    Node->data = i; 
    Node  = Node->next; 
} 

    for (i = 1; i<=10; i++){ 
    printf("\n Node->data:%d",Node->data); 
    Node = Node->next; 
    } 
return 0; 
} 
+3

你「可能」需要初始化和malloc的每everynode,不僅頭我的代碼。 –

+0

您創建一個單節點,然後嘗試循環9個不存在的節點。 –

+0

用'Node = Node-> next;'你去下一個節點,但是你忘記了列表開始的地方,通常叫做'head'。 –

回答

0

你只是一個節點分配的空間,但你是這樣在連接節點列表,試圖循環:

for (i = 1; i<=10; i++){ 
    Node->data = i; 
    Node  = Node->next; 
} 

當你這樣做 - Node = Node->next;Node可能指向任何地方。你可能會指向你不應該碰觸的記憶。

有一點要記住的是永遠不要放開手柄到列表頭部。通過保留副本來保持指向列表頭的指針。

Node = malloc(sizeof(Node_Struct)); 
Node_Struct *head = Node; 

通過使用malloc()爲每個節點分配空間。首先建立節點列表。要做到這一點的方法之一是這樣的:

for (i = 1; i<=10; i++){ 
    Node->next = malloc(sizeof (Node_Struct)); 
    Node  = Node->next; 
} 
Node->next = NULL // The last node should point to NULL 

之後,你可以設置所有節點的data領域的另一個循環,或將它們設置在同一個循環,而這樣做malloc()。由於您已經擁有清單head的句柄,因此您知道從哪裏開始。像這樣:

Node = head; 
i = 0; 
while (Node) { 
    Node->data = i++; 
    Node = Node->next; 
} 
+0

似乎你也有壓力快速給出答案。 'malloc'沒有初始化分配區域,並且有許多細微的錯誤 –

+1

@JacekCz我在哪裏說'malloc'初始化分配區域?並善意指出我所犯的微妙錯誤。總是樂於學習。 – babon

+0

@JacekCz我在等你指出我的微妙錯誤。 – babon

1

正如評論中的人所指出的,您只是將內存分配給頭節點而已。您需要爲每個要添加int的節點分配內存以用於循環。此外,您在每次迭代時都會向前移動Node指針,因此插入後將無法遍歷列表。跟蹤列表的頭部和尾部。執行以下操作:

保持頭和鏈表的尾巴:

Node_Struct* headNode = NULL, *tailNode = NULL; 
// head node 
headNode = tailNode = (Node_Struct*)malloc(sizeof(Node_Struct)); 

在迴路中的每個迭代分配內存。這是你的願望,你是否想要保持頭部節點或不。因此,更改代碼在這樣的循環:

for (i = 1; i<=10; i++) { 
    Node_Struct* newNode = (Node_Struct *)malloc(sizeof(Node_Struct)); 

    newNode->data = i; 
    newNode->next = NULL; 

    tailNode->next = newNode; 
    tailNode = newNode; 
} 

這之後,您可以通過在其他一些變量拷貝頭值迭代列表:

Node_Struct *tmpNode = headNode; 
    for (i = 1; i<=10; i++){ 
    printf("\n Node->data:%d",tmpNode->data); 
    tmpNode = tmpNode->next; 
    } 
1

你不會爲每個添加的節點分配內存。

如果使用你的循環則是足以讓這些小的改動

Node_Struct* Node = NULL; 
Node_Struct **current = &Node; 

for (i = 1; i <= 10; i++) { 
    *current = malloc(sizeof(Node_Struct)); 
    (*current)->data = i; 
    (*current)->next = NULL; 
    current = &(*current)->next; 
} 

current = &Node; 
for (i = 1; i <= 10; i++) { 
    printf("\n Node->data:%d", (*current)->data); 
    current = &(*current)->next; 
} 

考慮到,你應該退出程序前,免費爲節點的所有分配的內存。

0

首先我向你展示你的錯誤在哪裏和下面我已經重寫了你的代碼來解決你的問題。查看我的密碼並與您的密碼進行比較。希望這會幫助你學習數據結構。

讓我們來看看你的錯誤

#include<stdio.h> 
typedef struct Node 
{ 
    int data; 
    struct Node *next; 
}Node_Struct; 

int main(void) 
{ 
    int i =0; 
    Node_Struct* Node = NULL; 
    Node = (Node_Struct*)malloc(sizeof(Node_Struct)); 

for (i = 1; i<=10; i++){ 
    Node->data = i;/* till here everything fine */ 
    Node  = Node->next; /* now you are pointing to nowhere */ 
    /* in the next iteration of for loop Node->data = i will crash the application */ 
    /* you could have done it like below 
    Node->next = (Node_Struct*)malloc(sizeof(Node_Struct)); 
    Node  = Node->next; 
    Node->data = i; 
    Node->next = NULL 
    but here you lost the address of first node so all gone*/ 

} 

    for (i = 1; i<=10; i++){ 
    printf("\n Node->data:%d",Node->data); 
    Node = Node->next; 
    } 
    return 0; 
} 

現在看到下面

int main(void) 
{ 
    int i =0; 
    Node_Struct *Node = NULL; 
    Node_Struct *p = NULL; 

    for (i = 1; i<=10; i++){ 
    if (Node == NULL) 
    { 
     Node = (Node_Struct*)malloc(sizeof(Node_Struct)); 
     Node->data = i; 
     Node->next = NULL; 
     p = Node; /* p is pointing to the first Node of the list */ 
     } 
     else 
     { 
      p->next = (Node_Struct*)malloc(sizeof(Node_Struct)); 
      p = p->next; 
      p->data = i; 
      p->next = NULL; 
     } 
    } 

     p = Node; /* now p is pointing to first node of the link list */ 
     /* if you see above we always assign NULL to 'next' pointer so that the last node of the list pointing to NULL */ 
     /* Therefore in the below while loop we are searching the list untill we reach the last node */ 
     while(p != NULL) 
     { 
      printf("\n p->data:%d",p->data); 
      p = p->next; 
     } 
     return 0; 
    }