Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将给定链表的反向存储到另一个链表中_C_Data Structures_Linked List - Fatal编程技术网

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”指向反向链接列表的头部