0

I want to make a method that deletes elements called "archivo" from a list if cantLineas=0, the method deletes everything just right but I can't make it to put the pointer to NULL when the list has been deleted completely. Here is the code I've written:

struct archivo
{
    char * nombre;
    int cantLineas;
    archivo * sig;
};


void borrarArchivos(archivo * listaArchivos){
    archivo * ant=NULL;
    while(listaArchivos!=NULL){
        if(listaArchivos->cantLineas==0){
            if(ant!=NULL){
                ant->sig=listaArchivos->sig;
                delete listaArchivos;
                listaArchivos=ant->sig;
            }else{
                ant=listaArchivos;
                listaArchivos=listaArchivos->sig;
                delete ant;
                ant=NULL;
            } 
            if(ant==NULL && listaArchivos==NULL){
                listaArchivos=NULL;
            }
        }else{
            ant=listaArchivos;
            listaArchivos=listaArchivos->sig;
        }

    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
daisymoon
  • 49
  • 1
  • 8
  • Pass a double pointer `void borrarArchivos(archivo **listaArchivos)` – 101010 May 03 '14 at 18:30
  • 1
    Please just use `std::vector>` or `std::map`. Stop writing unmaintainable, broken code that has already been implemented better by your standard library. – Kerrek SB May 03 '14 at 18:32
  • Since this is part of a school project I can't use those librarys, but thanks any way. – daisymoon May 03 '14 at 19:25
  • Could be a school assigment, but you ( and mostly of the so called "C++ teachers") should understand that rejecting the stl in favor of low level programming is not a way to learn C++ at all. In fact what that practices encourage are the worst C++ practices, which are just C using cout and new, instead of printf/malloc. C++ is not C with classes, C++ is a multiparadigm language strongly based on its standard library – Manu343726 May 03 '14 at 19:35

1 Answers1

0

You are passing the pointer listArchivos by value, you have to pass it by reference. In order to achieve this you have to pass a double pointer or a reference to a pointer (see code below):

Double Pointer:

void borrarArchivos(archivo **listaArchivos) {
    archivo *ant=NULL;
    while(*listaArchivos!=NULL){
        if((*listaArchivos)->cantLineas==0){
            if(ant!=NULL){
                ant->sig=(*listaArchivos)->sig;
                delete *listaArchivos;
                *listaArchivos=ant->sig;
            }else{
                ant=*listaArchivos;
                *listaArchivos=(*listaArchivos)->sig;
                delete ant;
                ant=NULL;
            } 
            if(ant==NULL && *listaArchivos==NULL){
                *listaArchivos=NULL;
            }
        }else{
            ant=*listaArchivos;
            *listaArchivos=(*listaArchivos)->sig;
        }

    }
}

Reference to a Pointer:

void borrarArchivos(archivo *&listaArchivos){
    archivo * ant=NULL;
    while(listaArchivos!=NULL){
        if(listaArchivos->cantLineas==0){
            if(ant!=NULL){
                ant->sig=listaArchivos->sig;
                delete listaArchivos;
                listaArchivos=ant->sig;
            }else{
                ant=listaArchivos;
                listaArchivos=listaArchivos->sig;
                delete ant;
                ant=NULL;
            } 
            if(ant==NULL && listaArchivos==NULL){
                listaArchivos=NULL;
            }
        }else{
            ant=listaArchivos;
            listaArchivos=listaArchivos->sig;
        }

    }
}

Better yet, use the power of C++ STL and use smart pointers instead of raw C pointer and take advantage of STL containers like std::vector to manage and store your objects.

Community
  • 1
  • 1
101010
  • 41,839
  • 11
  • 94
  • 168
  • In this case every time I do `listaArchivos=listaArchivos->sig;` don't a lose the pointer to the first element of the list? – daisymoon May 03 '14 at 20:20