【发布时间】:2018-07-28 23:44:10
【问题描述】:
在标记为 dup 之前,我已阅读
In C, what does a variable declaration with two asterisks (**) mean?
I don't understand the implementation of inserting a new node in linked list
而且我仍在为双星号的逻辑步骤而苦苦挣扎。我知道在链表中我需要创建一个新节点,为其动态分配空间,然后将新节点重新标记为头部。
我只是不明白&head 和双星号之间函数的逻辑步骤。什么是指双星号的实现在这里如何工作以及如何工作?
void push(struct node** head_ref, int new_data)
{
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
push(&head, 2);
【问题讨论】:
-
使用笔和纸。手动执行代码。画图。写出变量的值。
-
这看起来不像标准的
push函数 - 它更像是createAndPush -
1.将地址作为指向指针的指针(因为它是一个节点) 2. 创建第二个节点 3. 使新节点指向(旧)头 4. 然后将 new_node 重新标记为头(或第一项在list) 那么由于节点结构的性质,双星号只是双星号?像 struct node ** 基本上是在声明节点的类型和数据类型?
-
函数调用像赋值一样工作:参数被赋值给参数。我们有
head_ref = &head,所以head_ref指向head。 -
@klutt 是的,实际上笔和纸对我有用。泰
标签: c linked-list dynamic-memory-allocation singly-linked-list