Home > OS >  Deleting node in list
Deleting node in list

Time:04-18

*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 cant delete the node fully, instead of this, its just replacing the value, but the node itself is not deleted

CodePudding user response:

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.

  • Related