Problems when deleting an element from a list within another chained list

Asked

Viewed 56 times

1

I’m having a problem removing an element, I have a function that is responsible to delete the element within double-chained lists.

How it works?

In the main function is called the name of an element(film) in which it must be removed. This element is allocated in a list of actors (there is a list of films for actors) and there is a list of films (with a list of actors who are part).

What I need to do?

I need to delete this element from the list of films that is contained in the list of actors and if the actor has only this film that is being removed it is necessary to also remove the actor in the list of actors.

What have I done so far? (briefly the main part that is in trouble)

.
.
.
typedef struct filme{
    char nome[P];
    char ano[14];
    struct duplaAt *listaAtores;
    struct duplaDir *listaDiretores;
    struct filme *next;
    struct filme *prev;
}filme;

typedef struct Ator{
    char nome[P];
    struct duplaFilm *listaFilmes;
    struct Ator *next;
    struct Ator *prev;
}ator;
typedef struct duplaAt{
    struct Ator *at;
    struct duplaAt *next;
    struct duplaAt *prev;
}duplaAt;
typedef struct duplaFilm{
    struct filme *fil;
    struct duplaFilm *next;
    struct duplaFilm *prev;
}duplaFilm;
void excluirAtRef(char auxInsert[P], struct Ator ** inicioAtores,struct Ator ** fimAtores)
{
    printf("\n TO AQUI! 1");
    struct Ator * atorHead=*inicioAtores;
    struct duplaFilm *refFilme;
    int band;
        while(atorHead!=NULL)
             {
                band=1;
                refFilme=atorHead->listaFilmes;
                while(refFilme!=NULL)
                 {
                    if(strcmp(refFilme->fil->nome,auxInsert)==0)
                 {
                    //flag
                      if(refFilme->prev==NULL && refFilme->next==NULL)
                      {
                       printf("\n TO AQUI! 2 :%s",atorHead->nome);
                       free(refFilme);
                        band=0; 
                      }
                       else if(refFilme->prev==NULL && refFilme->next!=NULL)
                      {

                       refFilme->next->prev=NULL;
                       free(refFilme);
                    printf("\n TO AQUI! 3: %s",atorHead->nome);

                      }
                else if(refFilme->prev!=NULL && refFilme->next!=NULL)
                 {
                     refFilme->prev->next = refFilme->next;
                    refFilme->next->prev = refFilme->prev;
                    free(refFilme);
                printf("\n TO AQUI! 4");

                 }
                else if(refFilme->prev!=NULL && refFilme->next==NULL)
                 {
                    refFilme->prev->next = NULL;

                    free(refFilme);
                    printf("\n TO AQUI! 5");
                 }



                 }
                    refFilme=refFilme->next;
                 }



                if(band==0)
                 {
                 if(atorHead->prev==NULL && atorHead->next==NULL)
                 {
                    *inicioAtores=NULL;
                    *fimAtores=NULL;
                    free(atorHead);
                    printf("\n ATOR TO AQUI!");
                 }
                else if(atorHead->prev==NULL && atorHead->next!=NULL)
                 {
                    atorHead->next->prev = NULL;
                    *inicioAtores = atorHead->next;

                    free(atorHead);
                    printf("\n ATOR TO AQUI! 2");
                 }
                else if(atorHead->prev!=NULL && atorHead->next!=NULL)
                 {
                    atorHead->prev->next = atorHead->next;
                    atorHead->next->prev = atorHead->prev;
                    free(atorHead); 
                    printf("\n ATOR TO AQUI! 3");
                 }
                else if(atorHead->prev!=NULL && atorHead->next==NULL)
                 {
                    atorHead->prev->next = NULL;
                    *fimAtores= atorHead->prev;

                    free(atorHead);
                    printf("\n ATOR TO AQUI! 4");
                 }

                 }

                atorHead=atorHead->next;
             }  

}

What’s the matter?

By removing the last element from the list of films in the actors, he is removing the actor. (And this theoretically shouldn’t be happening because the flag band is only being 0 when the film is unique on the list).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.