# Thread: Problem with dynamic lists

1. ## Problem with dynamic lists

Found the problem, it's in the list creation, I have something that's creating a loop in the list:
Code:
```int insere_col_inicial(lista_coelhos *base,int id_colonia){    lista_coelhos *aux,*procura,*seguinte;
int tamanho;
int x;
int y;

x = rand()*1.0*MAX_X/RAND_MAX;
y = rand()*1.0*MAX_X/RAND_MAX;
tamanho = ceil(abs((x -y)%30))+1;/*To avoid size zero of the colonies*/

aux=(lista_coelhos*)calloc(1,sizeof(lista_coelhos));
if(aux==NULL)
{
return -1;
}

aux->id_colonia=id_colonia;
aux->x=x;
aux->y=y;
aux->tamanho=tamanho;

procura=base;
seguinte=base->prox;

while(seguinte != NULL && tamanho>seguinte->tamanho)
{
procura=procura->prox;
seguinte=seguinte->prox;
}

aux->prox=seguinte;
procura->prox=aux;

return 0;
}```
here's where I call the function:
Code:
```for(i=1;i<=100;i++) /*To insert inicial colonies*/
{
nova_id=id_colonia+1;
insere_col_inicial(base,nova_id);
id_colonia=nova_id;
}```

2. Originally Posted by Chinchila
Found the problem
Excellent! Erm... what is your problem?

In any post like this always say (i) what you expect to happen and (ii) what act

3. The problem is, I want to create a list organized by tamanho (it's one of my variables), but something is making a loop in the list, when I reach the end of the list it has a pointer to an element in the middle and I don't understand why

4. The only obvious problem I see is that this function does not allow you to insert at the head of the list.

5. The head, you mean the end or the beggining? It's supposed to have the first element in blank, because of the searches