2017-10-05 90 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; 
    }