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?