问题
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