'Deleting node in list
*x = L->list[i]; /* Save the deleted element to parameter x */
for(j = i+1; j <= L->size-1; j++)
L->list[i] = L->list[i+1];
L->size--; /* The number of data elements is reduced by 1*/
return 1;
I can't delete the node fully, instead of this, its just replacing the value, but the node itself is not deleted
Solution 1:[1]
Within this for loop
for(j = i+1; j <= L->size-1; j++)
L->list[i] = L->list[i+1];
the variable i
used in this statement
L->list[i] = L->list[i+1];
is not being changed.
It seems you mean
for(j = i+1; j < L->size; j++)
L->list[j-1] = L->list[j];
If the array list
is allocated dynamically then you should reallocate it as for example
for(j = i+1; j < L->size; j++)
L->list[j-1] = L->list[j];
L->size--;
T *tmp = realloc( L->list, L->size * sizeof( *tmp ) );
if ( tmp != NULL ) L->list = tmp;
You will need to substitute T
with the actual type of elements of the array. I am using T
because it is unknown from your question what is the type of elements of the array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |