【问题标题】:Can't figure out why I am getting a segmentation fault when I try to append to the end of a linked list当我尝试附加到链表的末尾时,无法弄清楚为什么会出现分段错误
【发布时间】:2020-05-07 18:35:44
【问题描述】:

我正在尝试练习并熟悉链表,所以我制作了这个程序来尝试创建节点并在链表的末尾添加数据。

一切正常,直到到达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;
}

【问题讨论】:

  • 是的,这行得通!谢谢

标签: c data-structures struct linked-list singly-linked-list


【解决方案1】:

你使用的方法不正确。

您应该将数据附加到列表而不是指向节点的指针。

函数可以通过以下方式定义。

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;
}

在 main 你可以写

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] );
}

请注意,通常您应该在每个节点中复制传递的字符串。

【讨论】:

    猜你喜欢
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2015-10-26
    • 2021-11-03
    • 2013-04-08
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多