C 将给定链表的反向存储到另一个链表中
在这个问题中,我基本上把给定链表的倒数存储到另一个链表中。 下面是函数C 将给定链表的反向存储到另一个链表中,c,data-structures,linked-list,C,Data Structures,Linked List,在这个问题中,我基本上把给定链表的倒数存储到另一个链表中。 下面是函数 void copy(struct node** aref,struct node** bref) { struct node* first = *aref; struct node* second = *bref; while(first!=NULL) { struct node* tmp = (struct node*)malloc(sizeof(struct node));
void copy(struct node** aref,struct node** bref) {
struct node* first = *aref;
struct node* second = *bref;
while(first!=NULL) {
struct node* tmp = (struct node*)malloc(sizeof(struct node));
tmp->data = first->data;
tmp->next = second;
second = tmp;
first = first->next;
}
}
这不管用。但是,如果我用*bref替换second,它会工作。
为什么会这样?在while循环之后添加以下代码
while(first!=NULL)
{
struct node* tmp=(struct node*)malloc(sizeof(struct node));
tmp->data=first->data;
tmp->next=second;
second=tmp;
first=first->next;
}
/* CHANGE HERE */
*bref = second;
原因是您必须将“*bref”指向反向链接列表的头部