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
}
this is code to add node using push, but i was confuse in this part, Push(headRef, num);
,in here why not using ampersand for headref? if the argument is only headref, is it only copy the pointer to the push function?
headref is a pointer to a pointer to a node, if i call push with argument headref, is it only copy headref to the function and not modify the origin headref?, im not quite sure here, so headref-> head->node (NULL), current point to node(NULL),, then try to push num after headref?
Push, you can see that both functions have the same first parameterstruct node** headRefso you just passheadRef. In the second casecurrent->nextis a pointer, so&(current->next)is the pointer-to-pointer required. - Weather Vane