Node * head和Node ** head有什么区别? [英] What is the difference between Node *head versus Node **head?

查看:384
本文介绍了Node * head和Node ** head有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a C code to find the middle of linked list. I understood the logic but was unable to figure out how pointers are being used. What is the difference in the way Node *head and Node** head_ref work?

void middle(struct Node *head) ;

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

推荐答案

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

In the first function header, *head is a pointer to a node object which is allocated somewhere in memory:

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

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

while in the second function header, **head_ref is a pointer to a pointer to a node object somewhere in memory:

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

这是另一个间接层.为什么第二种构造是必要的?好吧,如果我想修改在函数范围之外分配的内容,则需要一个指向其内存位置的指针.在第一个示例中,我可以取消引用 * head 指针(带有 head-> )以访问内存中的基础 Node 对象,并进行对其进行修改或访问其属性.

This is another layer of indirection. Why is the second construct necessary? Well, if I want to modify something allocated outside of my function scope, I need a pointer to its memory location. In the first example, I can dereference the *head pointer (with head->) to access the underlying Node object in memory and make modifications to it or access its properties.

将此逻辑带入下一阶段的间接操作,如果我想将新的 Node 对象推到列表的开头以使其成为新的头,则需要修改指针本身.就像我想使用指针来操作 Node 对象时一样,现在我需要一个指向 * head 的指针(这恰好是指针而不是节点对象)进行修改.

Taking this logic to the next level of indirection, if I want to push a new Node object onto the front of the list to make it the new head, I need to modify the head pointer itself. Just as when I wanted to manipulate the Node object with a pointer, now I need a pointer to *head (which simply happens to be a pointer rather than a Node object) in order to modify it.

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

Here's the contents of push and a before/after function call:

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

这篇关于Node * head和Node ** head有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆