Removal of cells at the beginning of a list

2

I have this problem of removing an element from the beginning of a list, even doing the schematic in the drawing I could not. The strange thing is that it works with up to 3 elements, from the 4 cmd (which to using gcc to compile) to respond. Any idea?

    typedef struct evento_t {
    double tempo;
    char descricao[50];
    int id_origem, id_destino;
    struct evento_t *prox;
    } evento_t;

    void eventos_remover_inicio (evento_t **lista) {

    evento_t *auxiliar = *lista;

    if (*lista == NULL)
        printf ("Lista vazia\n");

    else 
        *lista = auxiliar->prox;

    free(auxiliar);

}
    
asked by anonymous 01.03.2014 / 19:03

3 answers

1

The only potential problem I see in this code is if you're calling something like this:

evento_t **lista;
lista = NULL;
eventos_remover_inicio(lista);

In this case, the function will be problematic because there is no integrity check of the *lista pointer before its use. If this is your problem I would recommend something like this:

void eventos_remover_inicio (evento_t **lista) {

    if(lista == NULL) 
    {
        return;
    }

    evento_t *auxiliar = *lista;

    if (*lista == NULL)
         printf ("Lista vazia\n");

    else 
         *lista = auxiliar->prox;

    free(auxiliar);
}

But it would be much easier to understand the problem if you post the code that uses this function and is giving trouble.

    
04.03.2014 / 01:47
1

I did not understand the need to use pointer to pointer in 'list'. What characterizes a list is the fact that its elements have pointers to elements of the same type; struct event_t * list; list = malloc (sizeof (event_t), 1); // or something like that, I forgot how to use malloc. list-> prox = malloc (sizeof (event_t), 1);

    
10.03.2014 / 13:16
0

Hello, Try to build on this example.

typedef struct node{
    int value;
    struct node *next;
}Node;

// Como declarar o root da lista ligada
Node *root = NULL;

void removeFirstNode(Node *list){
    Node *aux = list;
    if(list == NULL){
        return NULL;
    }else{
        list = aux->next;
        free(aux);
    }
}
    
07.03.2014 / 22:39