链表——插入多个节点

Linked List - insert more than one node

我是 c++ 新手,很难理解链表中的插入。这是我到目前为止一直在处理的插入内容,我只是在添加多个节点时遇到了问题。

struct node *temp, *x, *y;
temp = create_node(a, b, c);
x = begin;
if (begin == NULL)
{
    begin = temp;
    temp->next = NULL;
}
else
{
    y = temp;
    temp->next = NULL;
}

你代码的最后一部分(当列表不为空时插入)没有正确调整指针。以下代码修复了该问题:

struct node *temp;
temp = create_node(a, b, c);
if (begin == NULL)
{
    begin = temp;
    temp->next = NULL;
}
else
{
    temp->next = begin;
    begin = temp;
}