【问题标题】:Head Node not updated outside Scope [Single Linked List]头节点未在范围外更新 [单链表]
【发布时间】:2022-01-09 13:31:05
【问题描述】:

我已经解释了我目前陷入困境的地方。

下面这个结构是我的节点

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

struct node *head;

我已经为头节点分配了内存。

head = malloc(sizeof(struct node));

并通过

创建了一堆与之链接的其他节点
struct node *temp;
for (int i=0; i < no_of_nodes - 1; i++)
    {
        struct node *n = malloc(sizeof(struct node));
        temp -> next = n;
        printf("Enter Node %d data : ", i+1);
        scanf("%d", &(n -> data));

        temp = n;
    }
    temp -> next = NULL; 

然后是我的printNodes函数

void printNodes(struct node *n)
{
    //printf("%d", n -> data);
    while(n != NULL)
    {   
        if (n -> next != NULL)
            printf("%d%s", n -> data, " -> ");
        else
            printf("%d%s", n -> data, " -> NULL\n");
        n = n->next;
    }
}

输出将如下所示:6 -> 7 -> 8 -> 9 -> 3 -> NULL

现在,我正在尝试在头节点插入一个节点,即它成为头节点,原始头成为第二个节点。

printf("Enter the head position (obviously 0) : ");
scanf("%d", &pos);
insertNode(head, pos);
printNodes(head);

插入节点函数:

void insertNode (struct node *head, int pos)
{
    struct node *n;
    n = malloc(sizeof(struct node));
    printf("Enter the Node data : ");
    scanf("%d", &(n -> data));

    if (pos == 0)
    {
        printf("Pos was 0, so HEAD\n");
        n -> next = head;
        head = n;
        printNodes(head);
    }
}

好的,所以问题是,printNodes(head) 实际上按预期打印.. 但是 printNodes(head);after insertNodes(head, pos) 不打印更新的链接列表, 为什么头部没有更新? 我哪里做错了?

我得到的输出是, 输出:

输入Head位置(显然是0):0

输入节点数据:34

Pos 为 0,所以 HEAD

34 -> 6 -> 7 -> 8 -> 9 -> 3 -> NULL [from printNodes(head) inside insertNodes(head, pos)]

6 -> 7 -> 8 -> 9 -> 3 -> NULL [from printNodes(head) after insertNodes(head, pos)]

如果有人想查看完整代码, here你去。

任何帮助表示赞赏.. 提前谢谢你。

【问题讨论】:

    标签: c struct pass-by-reference singly-linked-list function-definition


    【解决方案1】:

    函数insertNode 按值接受指向头节点的指针。也就是说,它处理指向头节点的原始指针值的副本。更改副本不会反映在原始指针上。原来的指针保持不变。

    你需要通过引用传递指针。

    在 C 中,通过引用传递对象意味着通过指向它的指针间接传递它。解除对指针的引用,您将获得对被引用对象的直接访问。

    函数至少可以这样看

    int insertNode( struct node **head, size_t pos )
    {
        struct node *n = malloc( sizeof( struct node ) );
        int success = n != NULL;
    
        if ( success )
        {
            printf( "Enter the Node data : " );
            scanf( "%d", &n->data );
    
            while ( *head != NULL && pos-- )
            {
                head = &( *head )->next;
            }
    
            n->next = *head;
            *head = n;
        }
    
        return success;
    }
    

    函数的调用方式如下

    insertNode( &head, pos );
    

    【讨论】:

    • 感谢您的回答,我会尝试一下,但我仍然无法将头放在指向指针的指针上......无论我读了多少次,这对我来说似乎很困惑.还有为什么我们使用 size_t pos ?它会自动适应 pos 的值吗?
    • @adenosinetp10 size_t 是无符号整数类型。所以类型的对象不能是负数。您无需在函数内检查 pos 是否为负数。
    • @adenosinetp10 例如,如果您有类似 int x = 10; 的声明int *p = &x;然后您可以使用指针 p 更改对象 x,例如 *p = 20;如果你有一个指针 struct node *head = NULL; 也一样。和一个指向指针结构节点的指针**p = &head;然后您可以使用指针 p 更改指针头,例如 *p = malloc( sizeof( struct node ) );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多