英文:
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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论