克隆 link 列表时如何将内存分配给 tail->next 指针
how memory is assigned to the tail->next pointer while cloning the link list
下面是复制 link 列表的工作函数,只需将原始 link 列表的地址传递给此函数,它将创建一个副本和 return 头部.
我不明白内存是如何分配给 tail->next 指针以及它如何仍然指向 head_copy 指针。
尾巴=尾巴->下一个;现在 tail 指向使用 malloc 分配给它的新内存,但它仍然指向 head_copy.
任何人都可以帮助我理解这段代码的流程。
struct node
{
int data;
struct node* next; //Pointing back to the same structure.
};
struct node* copy_link_list(struct node** head)
{
struct node* head_copy = NULL;
struct node* tail = NULL;
while (*head != NULL)
{
//printf("data in copy is %d and local_variable addr is %p\n",temp->data,&local);
if (head_copy == NULL)
{
head_copy = malloc(sizeof(struct node));
head_copy->data = (*head)->data;
head_copy->next = NULL;
tail = head_copy;
}
else
{
//printf("1. tail /// tail_of_data /// tail_of_next %p %d %p\n",&tail,tail->data,&(tail->next));
printf("head_copy head_copy_of_data and head_copy_of_next %p %d %p\n",&head_copy,head_copy->data,(head_copy->next));
tail->next = malloc(sizeof(struct node));
printf("2. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail = tail->next;
printf("3. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail->data = (*head)->data;
tail->next = NULL;
}
*head = (*head)->next;
}
return head_copy;
}
在 while 循环的第一次迭代中,指针 head_copy
等于 NULL
。所以这个if语句被执行了。
if (head_copy == NULL)
{
head_copy = malloc(sizeof(struct node));
head_copy->data = (*head)->data;
head_copy->next = NULL;
tail = head_copy;
}
现在为一个节点分配了内存并将其地址分配给指针head_copy
。由于此分配,指针 tail
也指向同一节点
tail = head_copy;
在循环的第二次迭代中,指针 head_copy
已经不等于 NULL
。所以执行了else语句。
else
{
//printf("1. tail /// tail_of_data /// tail_of_next %p %d %p\n",&tail,tail->data,&(tail->next));
printf("head_copy head_copy_of_data and head_copy_of_next %p %d %p\n",&head_copy,head_copy->data,(head_copy->next));
tail->next = malloc(sizeof(struct node));
printf("2. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail = tail->next;
printf("3. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail->data = (*head)->data;
tail->next = NULL;
}
在 else 语句的开头,head_copy
和 tail
彼此相等。然后 tail
被重新赋值为 tail->next
tail = tail->next;
即tail
现在指向创建列表的当前最后一个节点。所以从这里开始,指针 tail
永远不会等于指针 head_copy
中存储的值。它指向当前创建的新节点,即创建列表的最后一个节点。
注意函数可以看起来更简单。例如,不需要通过引用传递复制列表的指针头。
struct node* copy_link_list( const struct node *head )
{
struct node *head_copy = NULL;
struct node **tail = &head_copy;
for ( ; head != NULL; head = head->next )
{
*tail = malloc( sizeof( struct node ) );
( *tail )->data = head->data;
( *tail )->next = NULL;
tail = &( *tail )->next;
}
return head_copy;
}
下面是复制 link 列表的工作函数,只需将原始 link 列表的地址传递给此函数,它将创建一个副本和 return 头部.
我不明白内存是如何分配给 tail->next 指针以及它如何仍然指向 head_copy 指针。 尾巴=尾巴->下一个;现在 tail 指向使用 malloc 分配给它的新内存,但它仍然指向 head_copy.
任何人都可以帮助我理解这段代码的流程。
struct node
{
int data;
struct node* next; //Pointing back to the same structure.
};
struct node* copy_link_list(struct node** head)
{
struct node* head_copy = NULL;
struct node* tail = NULL;
while (*head != NULL)
{
//printf("data in copy is %d and local_variable addr is %p\n",temp->data,&local);
if (head_copy == NULL)
{
head_copy = malloc(sizeof(struct node));
head_copy->data = (*head)->data;
head_copy->next = NULL;
tail = head_copy;
}
else
{
//printf("1. tail /// tail_of_data /// tail_of_next %p %d %p\n",&tail,tail->data,&(tail->next));
printf("head_copy head_copy_of_data and head_copy_of_next %p %d %p\n",&head_copy,head_copy->data,(head_copy->next));
tail->next = malloc(sizeof(struct node));
printf("2. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail = tail->next;
printf("3. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail->data = (*head)->data;
tail->next = NULL;
}
*head = (*head)->next;
}
return head_copy;
}
在 while 循环的第一次迭代中,指针 head_copy
等于 NULL
。所以这个if语句被执行了。
if (head_copy == NULL)
{
head_copy = malloc(sizeof(struct node));
head_copy->data = (*head)->data;
head_copy->next = NULL;
tail = head_copy;
}
现在为一个节点分配了内存并将其地址分配给指针head_copy
。由于此分配,指针 tail
也指向同一节点
tail = head_copy;
在循环的第二次迭代中,指针 head_copy
已经不等于 NULL
。所以执行了else语句。
else
{
//printf("1. tail /// tail_of_data /// tail_of_next %p %d %p\n",&tail,tail->data,&(tail->next));
printf("head_copy head_copy_of_data and head_copy_of_next %p %d %p\n",&head_copy,head_copy->data,(head_copy->next));
tail->next = malloc(sizeof(struct node));
printf("2. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail = tail->next;
printf("3. tail /// tail_of_data /// tail_of_next %p %d %p %p\n",&tail,tail->data,&(tail->next),tail->next);
tail->data = (*head)->data;
tail->next = NULL;
}
在 else 语句的开头,head_copy
和 tail
彼此相等。然后 tail
被重新赋值为 tail->next
tail = tail->next;
即tail
现在指向创建列表的当前最后一个节点。所以从这里开始,指针 tail
永远不会等于指针 head_copy
中存储的值。它指向当前创建的新节点,即创建列表的最后一个节点。
注意函数可以看起来更简单。例如,不需要通过引用传递复制列表的指针头。
struct node* copy_link_list( const struct node *head )
{
struct node *head_copy = NULL;
struct node **tail = &head_copy;
for ( ; head != NULL; head = head->next )
{
*tail = malloc( sizeof( struct node ) );
( *tail )->data = head->data;
( *tail )->next = NULL;
tail = &( *tail )->next;
}
return head_copy;
}