【问题标题】:What is the difference between Node *head versus Node **head?Node *head 与 Node **head 有什么区别?
【发布时间】:2019-09-03 09:56:11
【问题描述】:

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

void middle(struct Node *head) ;

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

【问题讨论】:

  • 一个指向数据,另一个指向指针。
  • 在第二个函数中你需要传递一个指向指向链表头部的指针的引用,因为函数修改了头部的位置。 C 总是按值传递参数,所以如果你只是传递一个指针,从函数返回后你不会得到它的新值。
  • 另一种方法是传递 struct Node *head 但返回 struct Node * 而不是 void。这样您就可以将指针返回到新的头部。

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


【解决方案1】:

在第一个函数头中,*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

【讨论】:

    【解决方案2】:

    struct Node *head -> 这里的指针head是指向Linked List头部的指针。 Head是一个可以指向节点结构的指针。

    例如。

    如果你有这样的链表:- ____ _____ |_1__|--->|_2__|--->|_3__|--->....... 地址- 1000 1004 1008

    您的 Node *head 将是一个指针变量,保存值为 1 的节点的地址(头节点的地址,即存储值 1 的节点)。 head=1000的内容

    struct Node **head_ref -> 这里的head_ref是指向链表开头的指针。

    例如。

    如果你有这样的链表:- ____ _____ |_1__|--->|_2__|--->|_3__|--->....... 地址 - 1000 1004 1008 | *头 地址 - 5050 | **头

    您的 Node *head 将是一个指针变量,保存值为 1 的节点的地址(头节点的地址,即存储值 1 的节点)。 头部内容=1000 **head_ref 的内容将是头指针的地址,即 5050。

    **head_ref 用于间接引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2021-07-04
      • 2019-12-05
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多