无法弄清楚为什么当我尝试附加到链表的末尾时出现分段错误
Can't figure out why I am getting a segmentation fault when I try to append to the end of a linked list
我正在尝试练习并熟悉链表,所以我编写了这个程序来尝试创建节点并在接收数据时将其添加到链表的末尾。
一切正常,直到到达 add_node()
函数。
我已经修改了一百万次了,但我不知道哪里出了问题。
它编译得很好,但给我一个分段错误。
程序如下:
#include <stdio.h>
#include <stdlib.h>
struct node
{
char *card;
struct node *next;
};
typedef struct node node_t;
//print
void printlist(node_t *head)
{
node_t *temp = head;
while (temp != NULL)
{
printf("%s\n", temp->card);
temp = temp->next;
}
}
node_t *create_new_node(char *card)
{
node_t *result = malloc(sizeof(node_t));
result->card = card;
result->next = NULL;
return result;
}
node_t *insert_at_head(node_t **head, node_t *node_to_insert)
{
// have node_to_insert point to the head
node_to_insert->next = *head;
// now have the head point to the node_to_insert
*head = node_to_insert; // having this pointer requires the **head parameter
return node_to_insert;
}
// add new node at the end of the list
void add_node(node_t *head, node_t *new_node)
{
node_t *tmp = head;
while (tmp != NULL)
{
tmp = tmp->next;
}
tmp->next = new_node;
}
int main(void)
{
char *card_list[5] = {"counterspell", "black lotus", "giant growth", "mountain", "forest"};
int len = sizeof(card_list)/sizeof(card_list[0]);
node_t *head = NULL;
node_t *temporary;
for (int i = 0; i < len; i++)
{
temporary = create_new_node(card_list[i]);
if ( i == 0)
{
head = insert_at_head(&head, temporary);
}
else
{
add_node(head, temporary);
}
}
printlist(head);
return 0;
}
您使用的方法不正确。
您应该将数据附加到列表而不是指向节点的指针。
可以通过以下方式定义函数。
node_t * create_new_node( char *card )
{
node_t *result = malloc( sizeof( node_t ) );
if ( result != NULL )
{
result->card = card;
result->next = NULL;
}
return result;
}
这两个函数如果把指针传给头节点的话会更简单更不容易出错
int insert_at_head( node_t **head, char *card )
{
node_t *node_to_insert = create_new_node( card );
int success = node_to_insert != NULL;
if ( success )
{
node_to_insert->next = *head;
*head = node_to_insert;
}
return success;
}
// add new node at the end of the list
int add_node( node_t **head, char *card )
{
while ( *head != NULL )
{
head = &( *head )->next;
}
*head = create_new_node( card );
return *head != NULL;
}
在主要部分你可以写
char *card_list[] =
{
"counterspell", "black lotus", "giant growth", "mountain", "forest"
};
size_t len = sizeof( card_list ) / sizeof( card_list[0] );
node_t *head = NULL;
for ( size_t i = 0; i < len; i++ )
{
add_node( &head, card_list[i] );
}
注意,一般情况下,你应该在每个节点中复制传递的字符串。
我正在尝试练习并熟悉链表,所以我编写了这个程序来尝试创建节点并在接收数据时将其添加到链表的末尾。
一切正常,直到到达 add_node()
函数。
我已经修改了一百万次了,但我不知道哪里出了问题。
它编译得很好,但给我一个分段错误。
程序如下:
#include <stdio.h>
#include <stdlib.h>
struct node
{
char *card;
struct node *next;
};
typedef struct node node_t;
//print
void printlist(node_t *head)
{
node_t *temp = head;
while (temp != NULL)
{
printf("%s\n", temp->card);
temp = temp->next;
}
}
node_t *create_new_node(char *card)
{
node_t *result = malloc(sizeof(node_t));
result->card = card;
result->next = NULL;
return result;
}
node_t *insert_at_head(node_t **head, node_t *node_to_insert)
{
// have node_to_insert point to the head
node_to_insert->next = *head;
// now have the head point to the node_to_insert
*head = node_to_insert; // having this pointer requires the **head parameter
return node_to_insert;
}
// add new node at the end of the list
void add_node(node_t *head, node_t *new_node)
{
node_t *tmp = head;
while (tmp != NULL)
{
tmp = tmp->next;
}
tmp->next = new_node;
}
int main(void)
{
char *card_list[5] = {"counterspell", "black lotus", "giant growth", "mountain", "forest"};
int len = sizeof(card_list)/sizeof(card_list[0]);
node_t *head = NULL;
node_t *temporary;
for (int i = 0; i < len; i++)
{
temporary = create_new_node(card_list[i]);
if ( i == 0)
{
head = insert_at_head(&head, temporary);
}
else
{
add_node(head, temporary);
}
}
printlist(head);
return 0;
}
您使用的方法不正确。
您应该将数据附加到列表而不是指向节点的指针。
可以通过以下方式定义函数。
node_t * create_new_node( char *card )
{
node_t *result = malloc( sizeof( node_t ) );
if ( result != NULL )
{
result->card = card;
result->next = NULL;
}
return result;
}
这两个函数如果把指针传给头节点的话会更简单更不容易出错
int insert_at_head( node_t **head, char *card )
{
node_t *node_to_insert = create_new_node( card );
int success = node_to_insert != NULL;
if ( success )
{
node_to_insert->next = *head;
*head = node_to_insert;
}
return success;
}
// add new node at the end of the list
int add_node( node_t **head, char *card )
{
while ( *head != NULL )
{
head = &( *head )->next;
}
*head = create_new_node( card );
return *head != NULL;
}
在主要部分你可以写
char *card_list[] =
{
"counterspell", "black lotus", "giant growth", "mountain", "forest"
};
size_t len = sizeof( card_list ) / sizeof( card_list[0] );
node_t *head = NULL;
for ( size_t i = 0; i < len; i++ )
{
add_node( &head, card_list[i] );
}
注意,一般情况下,你应该在每个节点中复制传递的字符串。