Thread: Queue List strange behaviour

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    53

    Queue List strange behaviour

    Hi

    I have a queue with a strange behaviour because i'm inserting two records and when i print the list i have only one record, then, when i want to remove the first, the second is removed....so it's strange..

    Any one can give me a pick??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct animal{
        char especie[100],raca[100], dono[100];
        int idade;
        float preco;
    }INFO;
    
    
    typedef struct elemento{
        INFO nodo;
        struct elemento *seguinte;
        struct elemento *anterior;
    }ELEM;
    
    
    
    
    //Insert in queue
    int insAnimal(INFO info, ELEM **initLista, ELEM **fimLista)
    {
        ELEM *novo;
        novo=(ELEM*)malloc(sizeof(ELEM));
        if(novo==NULL)
        {
            printf("Erro na lista");
            return -1;
        }
        novo->nodo=info;
        novo->seguinte=NULL;
        novo->anterior=NULL;
        if(*initLista==NULL)
        {
            *initLista=novo;
            *fimLista=novo;
        }
        else
        {
            novo->seguinte=*initLista;
            (*initLista)->anterior=*initLista;
            *initLista=novo;
        }
        /*
        If i choose the end of queue
        novo->anterior=*fimLista;
        (*fimLista)->seguinte=*fimLista;
        *fimLista=novo;
        */
        return 0;
    }
    
    
    //Print list
    int listarInicio(ELEM *initLista)
    {
        ELEM *aux;
        if(initLista==NULL)
        {
            printf("Fila Vazia...");
            return -1;
        }
        aux=initLista;
        for(aux=initLista; aux!=NULL; aux=aux->seguinte)
        {
            printf("%s  %s  %s  %d  %.2f \n", aux->nodo.especie,aux->nodo.raca, aux->nodo.dono, aux->nodo.idade, aux->nodo.preco);
        }
        return 0;
    }
    //Next element to leave the queue
    
    
    int proximoAnimal(INFO info, ELEM **initLista, ELEM **fimLista)
    {
        ELEM *aux;
        aux = *fimLista;
        if(aux==NULL)
        {
            printf("Fila Vazia...");
            return -1;
        }
        if(aux->anterior==NULL)
        {
            printf("Elemento retirado... %s", aux->nodo.dono);
            *fimLista=aux->seguinte;
            if(*fimLista)
            *fimLista=aux->anterior=NULL;
        }
        else
        printf("Elemento n existe...");
        return 0;
    }
    
    
    //Sum of ages
    int totIdades(ELEM *initLista)
    {
        if(initLista==NULL)
        {
            printf("Fila Vazia...");
            return -1;
        }
        return(initLista->nodo.idade+totIdades(initLista->seguinte));
    }
    
    
    
    
    int main()
    {
        ELEM *initLista=NULL, *fimLista=NULL;
        INFO info;
        int i;
        for(i=0; i<2; i++)
        {
           printf("Specie: \n");
           fgets(info.especie,100, stdin);
            printf("Type: \n");
           fgets(info.raca,100, stdin);
            printf("Owner: \n");
           fgets(info.dono,100, stdin);
           printf("Age: \n");
           scanf("%d", &info.idade);
           getchar();
           printf("Price: \n");
           scanf("%f", &info.preco);
           getchar();
        }
        insAnimal(info,&initLista,&fimLista);
        printf("\nQueue List.");
        listarInicio(initLista);
       printf("\nNext element in Queue List.");
        proximoAnimal(info,&initLista,&fimLista);
        //totIdades(initLista);
        listarInicio(initLista);
        return 0;
    }
    Any help please?

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Fixed...

    Thank you all.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    I feel like deja vu all over again with this linked list code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour
    By cfanatic in forum C Programming
    Replies: 2
    Last Post: 07-16-2012, 06:41 AM
  2. strange behaviour
    By cfanatic in forum C Programming
    Replies: 9
    Last Post: 07-13-2012, 10:41 PM
  3. Strange behaviour of GCC
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 06:44 PM
  4. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 11:50 AM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM