Copy the linked list pointed by a parameter

旧巷老猫 提交于 2019-12-11 08:34:33

问题


I'm trying to implement a function which will make a copy of the list pointed by parameter. I store head pointers to nodes in some array and the parameter is just its index. This is my attempt:

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;
        }
}

Before I call copy function I allocate memory for new the head pointer in mentioned array (last index (amount of lists - 1)). Unfortunately, something is wrong in this function; does anyone know what should it look like?

来源:https://stackoverflow.com/questions/30695350/copy-the-linked-list-pointed-by-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!