Dynamic List with void pointer insert at start

Asked

Viewed 147 times

-1

 t_lista* Cria_Lista(int (*ptrFncEscreve)(void *ptrElemento))
 {
    t_lista *Lista;
    Lista = (t_lista*) malloc (sizeof(t_lista));
    Lista->qtde = 0;
    Lista->ptrFncEscreve = EscreveInteiro;
    return Lista;
}

t_nodeL* Cria_NoLista()
{
    t_nodeL *No;
    No = (t_nodeL*) malloc (sizeof(t_nodeL));//Se usa o tipo, e não a variavel
    return No;
}
int Insere_InicioLi (t_lista *Lista, void *x)
{
    t_nodeL *No = Cria_NoLista();
    No->val = x;
    No->Prox = Lista->inicio;
    No->Ant = NULL;
    No->Prox->Ant = No;
    Lista->inicio = No;
    Lista->qtde++;
    return 1;
 }

I can’t insert, what’s the problem?

  • 1

    You have to say what the problem is, young man. Are you making a mistake? Are you inserting it in the wrong place? Does the computer melt? Is there a voice from beyond screaming that something is wrong? Please be more specific.

1 answer

0


You are not checking if you are the first element of the list to be inserted.

int Insere_InicioLi (t_lista *Lista, void *x) 
{
    t_nodeL *No = Cria_NoLista();
    No->val = x;
    if(Lista->qtde == 0)
    {
        Lista->inicio = No;
        Lista->fim = No;
        No->Prox = NULL;
        No->Ant = NULL;
    }
    else
    {
        No->Prox = Lista->inicio;
        No->Ant = NULL;
        No->Prox->Ant = No;
        Lista->inicio = No;
     }
    Lista->qtde++;
    return 1;
}

Browser other questions tagged

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