struct node* AppendNode(struct node** headRef, int num) {
struct node* current = *headRef;
// special case for the empty list
if (current == NULL) {
Push(headRef, num); ->why not use & in front of headref?
} else {
// Locate the last node
while (current->next != NULL) {
current = current->next;
}
// Build the node after the last node
Push(&(current->next), num);
}
}
void Push(struct node** headRef, int data) {
struct node* newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = *headRef; // The '*' to dereferences back to the real head
*headRef = newNode; // ditto
}
这是使用push添加节点的代码,但我在这部分中混淆了Push(headRef,num);
,在这里为什么不使用&符号来进行headref?如果参数只是headref,它是否只将指针复制到push函数?
headref是一个指向节点指针的指针,如果我用参数headref调用push,它是否只将headref复制到函数而不修改origin headref ?,我在这里不太确定,所以headref-> head-> node(NULL),当前指向节点(NULL),,然后尝试在headref之后推送num?
答案 0 :(得分:0)
headref是指向节点的指针,如果我调用push with 参数headref,它是否只复制headref到函数而不是 修改原点headref?
要记住的一个方便之处是:如果要更改对象,则必须传入该对象的地址。
虽然您没有显示整个程序,但我认为假设您在调用DB::transaction(function()
{
DB::table('users')->update(array('votes' => 1));
DB::table('posts')->delete();
});
时将指针的地址传递给headRef
是安全的(我认为)。这个地址传递给AppendNode
,以便Push
可以取消引用一次并跳转到Push
的实际指针并在那里写一些东西。
如果您将headRef
声明为AppendNode
,那么您就会将AppendNode(struct node* headRef, int num)
传递给&headRef
。