function to insert a no in a linked list in C

Asked

Viewed 210 times

1

How to complete the missing code in lines with 1. 2. 3. 4.?

The goal is to add a new node with a given name and age at the end of the list.

NOTE: I know there are less confusing and more practical ways to solve this, and I know how to solve the problem differently, but I need to know how to solve with the code structured this way.

CODE:

struct lista {
    char nome[40];
    int idade;
    struct lista * prox;
};

int insere(char * nome, int idade, struct lista ** primeiro) {
    struct lista * no = malloc(sizeof(struct lista)), *atual = *primeiro, *ant = NULL;

    if (no == NULL)
        return 0;
    strcpy(no->nome, nome);
    no->idade = idade;
    no->prox = NULL;

    while (atual != NULL) {
        1.
        2.
    }

    if (ant != NULL)
        3.
    else
        4.
    return 1;
    }

int main(void) {

    int i;
    struct lista pessoas[] = { {"Jose", 18}, {"Rita", 20}, {"Paula", 19}, {"Ezequiel", 15}, {"Alexandre", 25}}, * minhalista, * primeiro;

    for (i = 0; i < sizeof(pessoas) / sizeof(pessoas[0]); i++) {
        if (insere(pessoas[i].nome, pessoas[i].idade, &minhalista) == 0)
            printf("Erro ar inserir %s\n", pessoas[i].nome);
    }

    primeiro = minhalista;

    while (primeiro != NULL) {
        printf("%s tem %d anos\n", primeiro->nome, primeiro->idade);
        primeiro = primeiro->prox;
    }
    return 0;
}
  • This code is very confusing...... Want to insert element at the beginning of the list or at the end?

  • at the end of the list. this code is taken from an exercise given to me by a teacher, and the statement is as follows: .

1 answer

0


The code for the missing lines is as follows:

int insere(char * nome, int idade, struct lista ** primeiro) {
    struct lista * no = malloc(sizeof(struct lista)), *atual = *primeiro, *ant = NULL;

    if (no == NULL)
        return 0;
    strcpy(no->nome, nome);
    no->idade = idade;
    no->prox = NULL;

    while (atual != NULL) {
        ant = atual; //1. 
        atual = atual->prox; //2.
    }

    if (ant != NULL)
        ant->prox = no; //3.
    else
        *primeiro = no; //4.
    return 1;
}

Lack in the main start the list with NULL other than the while will not work properly because the current(first) is never NULL:

int main(void) {
    struct lista pessoas[] = { {"Jose", 18}, {"Rita", 20}, {"Paula", 19}, {"Ezequiel", 15}, {"Alexandre", 25}}, * minhalista = NULL, * primeiro;
//  ---------------------------------------------------------------------------------------------------------------------------^^^^

Explaining the lines I’ve laid:

  1. Refresh the previous node to get behind the atual
  2. Refresh the current node to point to the next one
  3. When the ant is different from NULL the list is not empty and so the new node is placed at the end, after the ant for the atual was pointing to null.
  4. When the list is empty the new node becomes the first node in the list.

See how it works on ideone

  • Thank you very much, I realized how it is done, lines 1 and 2 were as I thought, I just did not know how to solve 3 and 4. But I also could not identify the error in (main). As in the statement of the exercise came this way I did not question. It helped me a lot, I thank again. :)

  • No problem, we’re here to help :)

Browser other questions tagged

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