链表头部的功能

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

the function of the head of a linked list

问题

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

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

  1. /* 3. 将新节点的下一个节点设置为头部 */
  2. new_node->next = (*head_ref);
  3. /* 4. 将头部移动到指向新节点 */
  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

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

I don't understand it well

答案1

得分: 1

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

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

  1. Node **head_ref Node *head Node anon1
  2. @ 0x1000 @ 0x3000
  3. +-----------+ +-----------+ +-----------------+
  4. | 0x1000 ---------->| 0x3000 ------------------------------------>| next: ... |
  5. +-----------+ +-----------+ | data: ... |
  6. +-----------------+
  7. Node *new_node Node anon2
  8. @ 0x4000
  9. +-----------+ +-----------------+
  10. | 0x4000 ---------->| next: NULL |
  11. +-----------+ | data: ... |
  12. +-----------------+

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

  1. Node **head_ref Node *head Node anon1
  2. @ 0x1000 @ 0x3000
  3. +-----------+ +-----------+ +-----------------+
  4. | 0x1000 ---------->| 0x3000 ---------------------------------+-->| next: ... |
  5. +-----------+ +-----------+ | | data: ... |
  6. | +-----------------+
  7. |
  8. Node *new_node Node anon2 |
  9. @ 0x4000 |
  10. +-----------+ +-----------------+ |
  11. | 0x4000 ---------->| next: 0x3000 -------+
  12. +-----------+ | data: ... |
  13. +-----------------+

(*head_ref) = new_node; 之后:

  1. Node **head_ref Node *head Node anon1
  2. @ 0x1000 @ 0x3000
  3. +-----------+ +-----------+ +-----------------+
  4. | 0x1000 ---------->| 0x4000 -------+ +-->| next: ... |
  5. +-----------+ +-----------+ | | | data: ... |
  6. | | +-----------------+
  7. | |
  8. Node *new_node | Node anon2 |
  9. | @ 0x4000 |
  10. +-----------+ | +-----------------+ |
  11. | 0x4000 -------+-->| next: 0x3000 -------+
  12. +-----------+ | data: ... |
  13. +-----------------+

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

  1. Node **head_ref Node *head
  2. @ 0x1000
  3. +-----------+ +-----------+
  4. | 0x1000 ---------->| 0x4000 -------+
  5. +-----------+ +-----------+ |
  6. |
  7. |
  8. Node *new_node | Node anon2
  9. | @ 0x4000
  10. +-----------+ | +-----------------+
  11. | 0x4000 -------+-->| next: NULL |
  12. +-----------+ | data: ... |
  13. +-----------------+

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

英文:

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.

  1. Node **head_ref Node *head Node anon1 Node anon0
  2. @ 0x1000 @ 0x3000 @ 0x2000
  3. +-----------+ +-----------+ +-----------------+ +-----------------+
  4. | 0x1000 ---------->| 0x3000 ---------->| next: 0x2000 ---------->| next: NULL |
  5. +-----------+ +-----------+ | data: ... | | data: ... |
  6. +-----------------+ +-----------------+

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

  1. Node **head_ref Node *head Node anon1
  2. @ 0x1000 @ 0x3000
  3. +-----------+ +-----------+ +-----------------+
  4. | 0x1000 ---------->| 0x3000 ------------------------------------>| next: ... |
  5. +-----------+ +-----------+ | data: ... |
  6. +-----------------+
  7. Node *new_node Node anon2
  8. @ 0x4000
  9. +-----------+ +-----------------+
  10. | 0x4000 ---------->| next: NULL |
  11. +-----------+ | data: ... |
  12. +-----------------+

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

  1. Node **head_ref Node *head Node anon1
  2. @ 0x1000 @ 0x3000
  3. +-----------+ +-----------+ +-----------------+
  4. | 0x1000 ---------->| 0x3000 ---------------------------------+-->| next: ... |
  5. +-----------+ +-----------+ | | data: ... |
  6. | +-----------------+
  7. |
  8. Node *new_node Node anon2 |
  9. @ 0x4000 |
  10. +-----------+ +-----------------+ |
  11. | 0x4000 ---------->| next: 0x3000 -------+
  12. +-----------+ | data: ... |
  13. +-----------------+

After (*head_ref) = new_node;

  1. Node **head_ref Node *head Node anon1
  2. @ 0x1000 @ 0x3000
  3. +-----------+ +-----------+ +-----------------+
  4. | 0x1000 ---------->| 0x4000 -------+ +-->| next: ... |
  5. +-----------+ +-----------+ | | | data: ... |
  6. | | +-----------------+
  7. | |
  8. Node *new_node | Node anon2 |
  9. | @ 0x4000 |
  10. +-----------+ | +-----------------+ |
  11. | 0x4000 -------+-->| next: 0x3000 -------+
  12. +-----------+ | data: ... |
  13. +-----------------+

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

  1. Node **head_ref Node *head
  2. @ 0x1000
  3. +-----------+ +-----------+
  4. | 0x1000 ---------->| 0x4000 -------+
  5. +-----------+ +-----------+ |
  6. |
  7. |
  8. Node *new_node | Node anon2
  9. | @ 0x4000
  10. +-----------+ | +-----------------+
  11. | 0x4000 -------+-->| next: NULL |
  12. +-----------+ | data: ... |
  13. +-----------------+

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:

确定