Delete list item FROM recursively

1

I have to delete the elements recursively from a list doubly linked from last to first. In addition, it has a pointer at the beginning and another at the end. I do not know what the error is.

void eliminar (lista DE, PPF){
  if (DE==NULL)
    return DE;
  else{
    lista temp = ppf.ultimo;
    ppf.ultimo = ppf.ultimo-> anterior;
    delete temp;
    return eliminar (temp, PPF);
  }
}
    
asked by Adri 15.11.2018 в 00:37
source

2 answers

1

Without having more code the problem is that the recursive call uses temp , element that you just deleted instead of using ppf .

void eliminar (lista DE, PPF){
  if (DE==NULL)
    return DE;
  else{
    lista temp = ppf.ultimo;
    ppf.ultimo = ppf.ultimo-> anterior;
    delete temp;                  // <<--- BORRAS temp
    return eliminar (temp, PPF);  // <<--- USAS temp
  }
}

The corrected code would look like this:

void eliminar (lista DE, PPF){
  if (DE==NULL)
    return DE;
  else{
    lista temp = ppf.ultimo;
    ppf.ultimo = ppf.ultimo-> anterior;
    delete temp;
    return eliminar (ppf, PPF); // <<--- USAS ppf
  }
}

On the other hand I do not finish understanding the utility of PPF . It is a variable that you do not use at all.

Additionally:

  • I strongly suggest using a more natural nomenclature for the variables. You do not do yourself any favors by using variables with cryptic names.
  • A node is not the same as a list. I suggest separating both concepts in independent structures / classes. The resulting code will be more natural and intuitive.
  • For future questions (including this one if the answers do not help you), it also includes the declaration of types and classes directly involved in the code that fails.
  • Pay attention when tagging the questions. delete is from C ++ not from C. If you mislabel a question may end up closed or without answers (at this point I would suggest you confirm if the question is well labeled)
answered by 15.11.2018 / 12:13
source
0

@Adri. I recommend that you edit your question, because delete is not an operator of C. I suspect this is C ++.

Likewise, I also recommend using keys in all if/else sentences. In the same way, we need to know how you have defined a lista , because by infused science I will not find out what type of data is ultimo , what% is anterior , or nothing.

Glancing at you without knowing more about the code, if you do delete temp and then call eliminar(temp, ppf) , obviously you'll be sending a pointer to NULL.

To top off the auction, the lista DE parameter is passed per copy, so when deleting, you never delete the item, you delete the copy of the item.

    
answered by 15.11.2018 в 10:45