在 C 中将节点附加到 link 列表(我快到了)

appending node to link list (I am almost there) in C

我想知道如何将节点附加到 link 列表。 我觉得我快到了,但是在盯着代码看了一会儿之后,我想不出它有什么问题。

我不得不提一下,在将第一个 int 添加到列表后,我遇到了分段错误...

typedef struct node
{
    int data;
    struct node* next;    
}
node;

node* head;

void append(node* pHead, int data)
{
    node* current = pHead;
    node* newNode = NULL;
    newNode = (node*)malloc(sizeof(node));
    newNode->data = data;
    newNode->next = NULL;

    if (current == NULL)
        pHead = newNode;    
    else
    {
        while (current->next != NULL)
            current = current->next;
    }
    current->next = newNode;       
}

int main(void)
{
    head = NULL;
    int howMany;
    int num;

    printf("how many?");
    scanf("%d", &howMany);

    for (int i = 0; i < howMany; i++)
    {
        printf("** %d ** number: ", i+1);
        scanf("%d", &num);
        append(head, num);
    }

我的错误在哪里?

即使当前为 NULL,您也会 current->next = newNode;

(current == NULL):

时,您必须 return 退出函数
if (current == NULL) {
    pHead = newNode;
    return;
}

else
{
    while (current->next != NULL)
        current = current->next;
}
current->next = newNode;

或将current->next = newNode;放在else语句中:

if (current == NULL) {
    pHead = newNode;
}

else
{
    while (current->next != NULL)
        current = current->next;
    current->next = newNode;
}

完整示例:

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node* next;
}
node;

node* head;

void append(node* pHead, int data)
{
    node* current = pHead;
    node* newNode = NULL;
    newNode = (node*)malloc(sizeof(node));
    newNode->data = data;
    newNode->next = NULL;

    if (current == NULL) {
        pHead = newNode;
        return;
    }

    else
    {
        while (current->next != NULL)
            current = current->next;
    }
    current->next = newNode;
}

int main(void)
{
    head = NULL;
    int howMany;
    int num;

    printf("how many?");
    scanf("%d", &howMany);

    for (int i = 0; i < howMany; i++)
    {
        printf("** %d ** number: ", i + 1);
        scanf("%d", &num);
        append(head, num);
    }
}

我认为问题出在这里:

if (current == NULL)
    pHead = newNode;    
else
{
    while (current->next != NULL)
        current = current->next;
}
current->next = newNode;

最后一行 current->next = newNode 应该在 else 主体内,否则当 current 为 NULL 时,您将尝试取消引用 NULL 指针。

所以,应该是:

if (current == NULL)
    pHead = newNode;    
else
{
    while (current->next != NULL)
        current = current->next;
    current->next = newNode;
}

另一个问题是你从不修改head。您只需修改 append() 内头指针的私有本地副本。此修改在 append() 之外不可见,因此程序最终会泄漏内存并且您永远无法访问列表(因为 head 始终为 NULL)。您可以将 pHead 设为 node **(这样可以看到对 *pHead 的修改),或者您可以在 append() 中修改 head,而不是传递它作为论据。这将起作用,因为您不会修改私有本地副本。 append() 应该是这样的:

void append(int data)
{
    node* current = head;
    node* newNode = NULL;
    newNode = (node*)malloc(sizeof(node));
    newNode->data = data;
    newNode->next = NULL;

    if (current == NULL)
        head = newNode;
    else
    {
        while (current->next != NULL)
            current = current->next;
        current->next = newNode;
    }
}