c - Node *head 与 Node **head 之间有什么区别?

标签 c pointers linked-list structure singly-linked-list

我正在编写一个 C 代码来查找链表的中间部分。我理解逻辑,但无法弄清楚指针是如何使用的。 Node *headNode** head_ref 工作方式有什么区别?

void middle(struct Node *head) ;

void push(struct Node** head_ref, int new_data) ; 

最佳答案

在第一个函数头中,*head是指向分配在内存中某处的节点对象的指针:

void middle(struct Node *head);
              _____________________
             |                     |
   *head --> |     Node object     |
             | [val=1][*next=NULL] |
             |_____________________|

在第二个函数头中,**head_ref是一个指向内存中某处节点对象的指针:

void push(struct Node** head_ref, int new_data); 
              _____________________
             |                     |
   *head --> |     Node object     |
     ^       | [val=1][*next=NULL] |
     |       |_____________________|
     |    
 **head_ref

这是另一层间接。为什么需要第二个构造?好吧,如果我想修改在函数作用域之外分配的内容,我需要一个指向其内存位置的指针。在第一个示例中,我可以取消引用 *head 指针(使用 head->)来访问内存中的底层 Node 对象并进行修改到它或访问它的属性。

将此逻辑带到下一个间接级别,如果我想将新的 Node 对象推送到列表的前面以使其成为新的头,我需要修改 head 指针本身。就像当我想用指针操作 Node 对象时一样,现在我需要一个指向 *head 的指针(它恰好是一个指针而不是 Node 对象)以便修改它。

以下是 push 的内容以及函数调用之前/之后:

void push(struct Node** head_ref, int new_data) {
    Node *new_head = malloc(sizeof(Node)); // allocate memory for the new head node
    new_head->data = new_data;             // set its value

    new_head->next = *head_ref;            // make it point to the 
                                           // old head pointer

    *head_ref = new_head;                  // make it the new head by modifying
                                           // the old head pointer directly
}
/* Before push */
              _____________________
             |                     |
   *head --> |     Node object     |
     ^       | [val=1][*next=NULL] |
     |       |_____________________|
     |    
 **head_ref
/* After calling push(&head, 2);
 *                    ^
 *                   `&` operator gets the memory address of `head`, 
 *                       which is a pointer.
 */

              _________________      _____________________
             |                 |    |                     |
   *head --> |   Node object   |    |      Node object    |
     ^       | [val=2] [*next]----->| [val=1][*next=NULL] |
     |       |_________________|    |_____________________|
     |    
 **head_ref

这是一个完整的示例:

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

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

void push(Node** head_ref, int new_data) {
    Node *new_head = malloc(sizeof(Node));
    new_head->data = new_data;
    new_head->next = *head_ref;
    *head_ref = new_head; 
}

void print(Node *head) {
    while (head) {
        printf("%d->", head->data);
        head = head->next;
    }

    puts("NULL");
}

void free_list(Node *head) {
    while (head) {
        Node *tmp = head;
        head = head->next;
        free(tmp);
    }
}

int main() {
    Node *head = malloc(sizeof(Node));
    head->next = NULL;
    head->data = 1;
    printf("Before push:\n");
    print(head);
    push(&head, 2);
    printf("\nAfter push:\n");
    print(head);
    free_list(head);
    return 0;
}

输出:

Before push:
1->NULL

After push:
2->1->NULL

关于c - Node *head 与 Node **head 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654934/

相关文章:

swift - 二元运算符 '!=' 不能应用于两个 ListNode swift

c - 在c中插入单链表

c - 为什么参数在这里使用指针的地址?

c - 在结构中使用多维数组

c++ - 如何使用 cairo 将图像写入 SVG 文件?

c++ - C++ 未处理的异常

java - 有没有办法在编译时为Java定义一个常量值

c++ - 指向抽象类的指针 vector ,用于访问派生类成员

c - 在C中,node.next->prev与node->next->prev相同吗?

swift - IteratorProtocol 需要 next() 函数来实现通用链表