复制参数指向的链接列表

时间:2015-06-07 15:33:57

标签: c list copy singly-linked-list

我正在尝试实现一个函数,该函数将生成参数指向的列表的副本。我将头指针存储到某些数组中的节点,参数只是它的索引。这是我的尝试:

void copy(node **array, int *amount_of_lists, int parameter)
{
    node *current = array[parameter];
    array[(*amount_of_lists) - 1] = malloc(sizeof(node));
    node *new_list = array[(*amount_of_lists) - 1];

        while(current->next != NULL)
        {
            new_list->next = malloc(sizeof(node));
            new_list->str = current->str;
            current = current->next;
            new_list = new_list->next;
        }
}

在我调用复制函数之前,我在所提到的数组中为新的头指针分配内存(最后一个索引(列表的数量 - 1))。不幸的是,这个功能出了问题;有谁知道它应该是什么样的?

0 个答案:

没有答案