Background is that I am experimenting with pointer-to-pointer in C by implementing a linked list. My question is regarding the difference in the two pieces of code and why the first one is giving expected output, but not the other one. Why does the first piece of code not advance head "outsite" the function which code 2 seems to do?
背景是我正在通过实现链表来试验C中的指针指针。我的问题是关于两段代码的区别以及为什么第一段代码给出了预期的输出,而不是另一条代码。为什么第一段代码没有推进“异地”代码2似乎做的功能?
void add_node(int x, Node **head){
if(*head == NULL){
Node *new_node = (Node*) malloc(sizeof(new_node));
new_node->x = x;
*head = new_node;
} else {
Node *n = *head;
while(n->next != NULL){
n = n->next;
}
Node *new_node = (Node*) malloc(sizeof(new_node));
new_node->x = x;
n->next = new_node;
}
}
Output is as expected if I add 4 elements and after each addition print the list: 1 | 12 | 123 | 1234
如果我添加4个元素,并且在每次添加后打印列表,则输出为预期:1 | 12 | 123 | 1234
void add_node(int x, Node **head){
if(*head == NULL){
Node *new_node = (Node*) malloc(sizeof(new_node));
new_node->x = x;
*head = new_node;
} else {
while((*head)->next != NULL){
*head = (*head)->next;
}
Node *new_node = (Node*) malloc(sizeof(new_node));
new_node->x = x;
(*head)->next = new_node;
}
}
Output is following: 1 | 12 | 23 | 34
输出如下:1 | 12 | 23 | 34
2 个解决方案
#1
2
In the first example you are using the pointer n to travel the linked list, you're assigning it to n->next, which is exactly what you want to do to travel a linked list. In the second example, you are changing what the head pointer is pointing to:
在第一个示例中,您使用指针n来移动链接列表,您将其分配给n-> next,这正是您要移动链接列表的目的。在第二个示例中,您正在更改头指针指向的内容:
*head = (*head)->next;
You're essentially moving the beginning of the linked list to another node, that's why you're having such behavior.
您实际上是将链表的开头移动到另一个节点,这就是您有这种行为的原因。
#2
0
Evaluate input 1,2,3 and focus on the head.
评估输入1,2,3并关注头部。
while((*head)->next != NULL){
*head = (*head)->next;
}
Node *new_node = (Node*) malloc(sizeof(new_node));
new_node->x = x;
(*head)->next = new_node;
leaves the head pointing somewhere, which your output is depicting ;)
让头指向某处,你的输出描绘;)
For input 1 and 1,2 the conditions are not met and you escape.
对于输入1和1,2,不满足条件并且您逃脱。
#1
2
In the first example you are using the pointer n to travel the linked list, you're assigning it to n->next, which is exactly what you want to do to travel a linked list. In the second example, you are changing what the head pointer is pointing to:
在第一个示例中,您使用指针n来移动链接列表,您将其分配给n-> next,这正是您要移动链接列表的目的。在第二个示例中,您正在更改头指针指向的内容:
*head = (*head)->next;
You're essentially moving the beginning of the linked list to another node, that's why you're having such behavior.
您实际上是将链表的开头移动到另一个节点,这就是您有这种行为的原因。
#2
0
Evaluate input 1,2,3 and focus on the head.
评估输入1,2,3并关注头部。
while((*head)->next != NULL){
*head = (*head)->next;
}
Node *new_node = (Node*) malloc(sizeof(new_node));
new_node->x = x;
(*head)->next = new_node;
leaves the head pointing somewhere, which your output is depicting ;)
让头指向某处,你的输出描绘;)
For input 1 and 1,2 the conditions are not met and you escape.
对于输入1和1,2,不满足条件并且您逃脱。