链表头部的功能

huangapple go评论71阅读模式
英文:

the function of the head of a linked list

问题

我不知道链表头的作用是什么,它是否包含数据,还是只用来指向列表中的第一项?

有时我发现一些资源将数据存储在头部,而不仅如此,这段代码在geeks4geeks上:

/* 3. 将新节点的下一个节点设置为头部 */
new_node->next = (*head_ref);

/* 4. 将头部移动到指向新节点 */
(*head_ref) = new_node;

我不太理解它。

英文:

I don't know what is the function of the linked list header, does it contain data, or just to point to the first item in the list?

sometimes I found some resources stock data in the head, more than that this code on geeks4geeks

    /* 3. Make next of new node as head */
    new_node->next = (*head_ref);
   
    /* 4. move the head to point to the new node */
    (*head_ref)    = new_node;

I don't understand it well

答案1

得分: 1

head_ref 是一个 Node**,也就是说 *head_ref(我在下面也称之为 head)是一个 Node*Node* 是指向节点的指针,而 *head_ref 具体是指向列表的第一个节点的指针。

在创建一个新节点之后,执行的代码如下:

Node **head_ref     Node *head                                    Node anon1
                    @ 0x1000                                      @ 0x3000
+-----------+       +-----------+                                 +-----------------+
| 0x1000 ---------->| 0x3000 ------------------------------------>| next: ...       |
+-----------+       +-----------+                                 | data: ...       |
                                                                  +-----------------+

                    Node *new_node      Node anon2
                                        @ 0x4000
                    +-----------+       +-----------------+
                    | 0x4000 ---------->| next: NULL      |
                    +-----------+       | data: ...       |
                                        +-----------------+

new_node->next = (*head_ref); 之后:

Node **head_ref     Node *head                                    Node anon1
                    @ 0x1000                                      @ 0x3000
+-----------+       +-----------+                                 +-----------------+
| 0x1000 ---------->| 0x3000 ---------------------------------+-->| next: ...       |
+-----------+       +-----------+                             |   | data: ...       |
                                                              |   +-----------------+
                                                              |
                    Node *new_node      Node anon2            |
                                        @ 0x4000              |
                    +-----------+       +-----------------+   |
                    | 0x4000 ---------->| next: 0x3000 -------+
                    +-----------+       | data: ...       |
                                        +-----------------+

(*head_ref) = new_node; 之后:

Node **head_ref     Node *head                                    Node anon1
                    @ 0x1000                                      @ 0x3000
+-----------+       +-----------+                                 +-----------------+
| 0x1000 ---------->| 0x4000 -------+                         +-->| next: ...       |
+-----------+       +-----------+   |                         |   | data: ...       |
                                    |                         |   +-----------------+
                                    |                         |
                    Node *new_node  |   Node anon2            |
                                    |   @ 0x4000              |
                    +-----------+   |   +-----------------+   |
                    | 0x4000 -------+-->| next: 0x3000 -------+
                    +-----------+       | data: ...       |
                                        +-----------------+

但如果列表为空,那么 *head_ref 最初包含 NULL,并且你会得到:

Node **head_ref     Node *head
                    @ 0x1000
+-----------+       +-----------+
| 0x1000 ---------->| 0x4000 -------+
+-----------+       +-----------+   |
                                    |
                                    |
                    Node *new_node  |   Node anon2
                                    |   @ 0x4000
                    +-----------+   |   +-----------------+
                    | 0x4000 -------+-->| next: NULL      |
                    +-----------+       | data: ...       |
                                        +-----------------+

完美!(所有地址完全是虚构的,当然。)

英文:

head_ref is a Node**, which is to say that *head_ref (which I also called head below) is a Node*. A Node* is a pointer to a node, and *head_ref is specifically a pointer to the first node of the list.

Node **head_ref     Node *head          Node anon1                Node anon0
                    @ 0x1000            @ 0x3000                  @ 0x2000
+-----------+       +-----------+       +-----------------+       +-----------------+
| 0x1000 ---------->| 0x3000 ---------->| next: 0x2000 ---------->| next: NULL      |
+-----------+       +-----------+       | data: ...       |       | data: ...       |
                                        +-----------------+       +-----------------+

The code you have there is executed after creating a new node.

Node **head_ref     Node *head                                    Node anon1
                    @ 0x1000                                      @ 0x3000
+-----------+       +-----------+                                 +-----------------+
| 0x1000 ---------->| 0x3000 ------------------------------------>| next: ...       |
+-----------+       +-----------+                                 | data: ...       |
                                                                  +-----------------+

                    Node *new_node      Node anon2
                                        @ 0x4000
                    +-----------+       +-----------------+
                    | 0x4000 ---------->| next: NULL      |
                    +-----------+       | data: ...       |
                                        +-----------------+

After new_node->next = (*head_ref);:

Node **head_ref     Node *head                                    Node anon1
                    @ 0x1000                                      @ 0x3000
+-----------+       +-----------+                                 +-----------------+
| 0x1000 ---------->| 0x3000 ---------------------------------+-->| next: ...       |
+-----------+       +-----------+                             |   | data: ...       |
                                                              |   +-----------------+
                                                              |
                    Node *new_node      Node anon2            |
                                        @ 0x4000              |
                    +-----------+       +-----------------+   |
                    | 0x4000 ---------->| next: 0x3000 -------+
                    +-----------+       | data: ...       |
                                        +-----------------+

After (*head_ref) = new_node;

Node **head_ref     Node *head                                    Node anon1
                    @ 0x1000                                      @ 0x3000
+-----------+       +-----------+                                 +-----------------+
| 0x1000 ---------->| 0x4000 -------+                         +-->| next: ...       |
+-----------+       +-----------+   |                         |   | data: ...       |
                                    |                         |   +-----------------+
                                    |                         |
                    Node *new_node  |   Node anon2            |
                                    |   @ 0x4000              |
                    +-----------+   |   +-----------------+   |
                    | 0x4000 -------+-->| next: 0x3000 -------+
                    +-----------+       | data: ...       |
                                        +-----------------+

But what if the list is empty? Then *head_ref starts off containing NULL, and you end up with

Node **head_ref     Node *head
                    @ 0x1000
+-----------+       +-----------+
| 0x1000 ---------->| 0x4000 -------+
+-----------+       +-----------+   |
                                    |
                                    |
                    Node *new_node  |   Node anon2
                                    |   @ 0x4000
                    +-----------+   |   +-----------------+
                    | 0x4000 -------+-->| next: NULL      |
                    +-----------+       | data: ...       |
                                        +-----------------+

Perfect!

(All addresses are completely fictional, of course.)

huangapple
  • 本文由 发表于 2023年2月16日 07:01:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定