Thread: Delete items in a substructure - Linked List - Two Levels

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Delete items in a substructure - Linked List - Two Levels

    I am try to delete a sub-struct in a two level linkedlist.


    I am able to delete correct if i delete the 1º item, if i delete other items the list don´t work properly.

    Code:
    typedef struct SPECIES species, *pspecies;
    typedef struct ANIMAL no, *pno;
     
    struct SPECIES {
        char especie[MAX];
        char idalphanumeric[MAX]; 
        long int nranimais; 
        pno listadeanimais;
        pspecies prox;
    };
     
    struct ANIMAL {
        char especie[MAX];
        int id;
        char nome[MAX]; 
        int peso;
        char nomelocal[MAX]; 
        pno prox; 
    };
    
    
    pspecies apaga_animal_zoo(pspecies list) {
    
    
        pno aux, anterior;
    
    
        int i = 1380;
    
    
        while (list != NULL) {
            aux = list->listadeanimais;
            anterior = NULL;
            while (aux != NULL) {
                if (aux->id == i) {
    
    
                    if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
                        list->listadeanimais = aux->prox;
                        return list;
                    } else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
                        anterior->prox = aux->prox;
                         list->listadeanimais = aux->prox;
                    }
    
    
                    printf("The Animal with the name %s was remove\n", aux->nome);
                   
    
    
                    free(aux);
                    return list;
    
    
                    //aux = aux->prox;
                } else {
                    anterior = aux;
                    aux = aux->prox;
                }
    
    
            }
            list = list->prox;
        }
    
    
        if (aux == NULL) { // The Animal don´t existe.
            puts("Animal nao existe sera enviado para o MENU");
            return list;
        }
    
    
    
    
        puts("The List is empty");
    
    
        return list;
    }
    Last edited by thinkabout; 05-28-2017 at 12:20 PM.

  2. #2
    Registered User
    Join Date
    May 2017
    Posts
    61
    I think i got it.

    Please feel free to give me adivice anyway.

    Code:
    pspecies apaga_animal_zoo(pspecies list) {
    
    
        pno aux, anterior;
    
    
        int i = 1396;
    
    
        while (list != NULL) {
            aux = list->listadeanimais;
            anterior = NULL;
            while (aux != NULL) {
                if (aux->id == i) {
    
    
                    if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
                        list->listadeanimais = aux->prox;
                        return list;
                    } else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
                        anterior->prox = aux->prox;
                        list->listadeanimais = aux->prox;
                    }
    
    
                    printf("The Animal with the name %s was remove\n", aux->nome);
    
    
    
    
                    free(aux);
                    return list;
    
    
                    //aux = aux->prox;
                }
                anterior = aux;
                aux = aux->prox;
    
    
            }
            list = list->prox;
        }
    
    
        if (aux == NULL) { // The Animal don´t existe.
            puts("Animal nao existe sera enviado para o MENU");
            return list;
        }
    
    
    
    
        puts("The List is empty");
    
    
        return list;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,626
    > Record and Reading from a Binary File - LinkedList - Two Levels
    Well you've still got the messy typedef's

    > typedef struct SPECIES species, *pspecies;
    > typedef struct ANIMAL no, *pno;
    At least species is somewhat related, but how the hell does 'no' relate to ANIMAL.

    Code:
    struct ANIMAL;
    
    typedef struct SPECIES {
        char especie[MAX];
        char idalphanumeric[MAX]; 
        long int nranimais; 
        struct ANIMAL *listadeanimais;
        struct SPECIES *prox;
    } species;
      
    typedef struct ANIMAL {
        char especie[MAX];
        int id;
        char nome[MAX]; 
        int peso;
        char nomelocal[MAX]; 
        struct ANIMAL *prox; 
    } animal;
     
     
    species * apaga_animal_zoo(species * list) {
        animal *aux, *anterior;
     
        int i = 1380;
     
        while (list != NULL) {
            aux = list->listadeanimais;
            anterior = NULL;
            while (aux != NULL) {
                if (aux->id == i) {
                    if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
                        list->listadeanimais = aux->prox;
                        //!! This misses the 'print and free'
                        return list;
                    } else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
                        anterior->prox = aux->prox;
                        //!! This causes all the animals up to this point to be lost!  Delete this line
                        list->listadeanimais = aux->prox;
                    }
     
                    printf("The Animal with the name %s was remove\n", aux->nome);
                    //!! what about list->nranimais ?
                    free(aux);
                    return list;
                    //aux = aux->prox;
                } else {
                    anterior = aux;
                    aux = aux->prox;
                }
            }
            list = list->prox;
        }
     
     
        if (aux == NULL) { // The Animal don´t existe.
            puts("Animal nao existe sera enviado para o MENU");
            return list;
        }
     
        puts("The List is empty");
     
        return list;
    }
    Read the //!! comments.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    61
    Something is missing.

    Even now if the animal doesn´t exist and i return "list" it came empty when i try to print with again.

    Code:
    species * apaga_animal_zoo(species * list) {
       
        no  *aux, *anterior;
        
        int i = 1620;
    
    
        while (list != NULL) {
            aux = list->listadeanimais;
            anterior = NULL;
            while (aux != NULL) {
                if (aux->id == i) {
                    if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
                        list->listadeanimais = aux->prox;
                    } else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
                        anterior->prox = aux->prox;
                        //   list->listadeanimais = aux->prox;
                    }
    
    
                    printf("The Animal with the name %s was remove\n", aux->nome);
                    list->nranimais = (list->nranimais - 1);
                    free(aux);
                    return list;
                }
                anterior = aux;
                aux = aux->prox;
            }
            list = list->prox;
        }
    
    
        if (aux == NULL) { // The Animal does not exists.
            puts("Animal does not exist you are going to the MAIN MENU");
            return list;
        }
    
    
        puts("The List is empty");
    
    
        return list;
    }
    Delete items in a substructure - Linked List - Two Levels-output-jpg
    Last edited by thinkabout; 05-28-2017 at 02:42 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,626
    Why do you have 'return list' all over the place when you've messed up the list with
    list = list->prox;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by Salem View Post
    Why do you have 'return list' all over the place when you've messed up the list with
    list = list->prox;
    Sorry

    That was a basic mistake , i think i manage it now.

    I am working and don´t have time to do extensive tests.

    I will pick up the code ASAP, the next step will be to delete the specie if the nranimais will be zero.


    Code:
    species * apaga_animal_zoo(species * listoriginal) {
    
    
        species *list = listoriginal;
    
    
        no *aux, *anterior;
    
    
        int i = 1932;
    
    
        while (list != NULL) {
            aux = list->listadeanimais;
            anterior = NULL;
            while (aux != NULL) {
                if (aux->id == i) {
                    if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
                        list->listadeanimais = aux->prox;
                    } else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
                        anterior->prox = aux->prox;
                    }
    
    
                    printf("The Animal with the name %s was remove\n", aux->nome);
                    list->nranimais = (list->nranimais - 1);
                    free(aux);
                    return listoriginal;
                }
                anterior = aux;
                aux = aux->prox;
            }
            list = list->prox;
        }
    
    
    
    
        if (aux == NULL) { // The Animal does not exist.
            puts("Animal nao existe sera enviado para o MENU");
            return listoriginal;
        }
    
    
        if (list == NULL) {
            puts("The List is empty");
            return listoriginal;
        }
    }

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    61
    Any adivces ?

    Now if then numbers of animal is 0 i delete the "specie" too.

    Code:
    species * apaga_animal_zoo(species * listoriginal) {
    
    
        species *list = listoriginal;
        species *before = NULL;
    
    
        no *aux, *anterior;
    
    
        int i = 2158;
    
    
        while (list != NULL) {
            aux = list->listadeanimais;
            anterior = NULL;
            while (aux != NULL) {
                if (aux->id == i) {
                    if (anterior == NULL) { // Se for o primeiro animal, vou passar a cabeça da lista para o nó a seguir
                        list->listadeanimais = aux->prox;
                    } else {// Não sendo o animal da lista vou ligar o nó "n-1" ao nó "n+1", apagando o nó "n"
                        anterior->prox = aux->prox;
                    }
    
    
                    printf("The Animal with the name %s was remove\n", aux->nome);
    
    
                    printf("\nThe specie stay with %ld", list->nranimais);
                    free(aux);
    
    
                    list->nranimais = (list->nranimais - 1);
                    if (list->nranimais == 0) {
                        if (before == NULL) {// If is the frist node
                            list = list->prox;
                        } else {
                            before->prox = list->prox;
                        }
    
    
                        free(list);
                        return listoriginal;
                    }
    
    
                    return listoriginal;
                }
                anterior = aux;
                aux = aux->prox;
            }
            before = list;
            list = list->prox;
    
    
        }
    
    
    
    
        if (aux == NULL) { // The Animal does not exist.
            puts("Animal nao existe sera enviado para o MENU");
            return listoriginal;
        }
    
    
        if (list == NULL) {
            puts("The List is empty");
            return listoriginal;
        }
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,626
    You need to sort out your return list statements, since you're now potentially returning a new head to your species list.

    A few minor tweaks between lines 36 and 43.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding storing items into a linked list.
    By jsuite in forum C Programming
    Replies: 10
    Last Post: 11-29-2012, 11:03 AM
  2. Linked List for items???
    By voidpain() in forum C Programming
    Replies: 7
    Last Post: 08-08-2011, 09:36 PM
  3. how to reverse items in linked list?
    By ilikeapplepie in forum C Programming
    Replies: 8
    Last Post: 04-09-2011, 11:15 PM
  4. Delete All Items In A Linked List
    By Khellendros in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2009, 01:22 AM
  5. modifying items in a linked list?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-14-2002, 01:37 PM

Tags for this Thread