List chained insertion at end

Asked

Viewed 407 times

0

Hello I would like to know what to insert in the conditional within the function, to add the elements at the end of the list while they are different from 0... I’ve tried several ways and I couldn’t...

    typedef struct node{ // Struct para ser usada como nó...
        int data;
        struct node *next;  
    };

    void insert(node *lista, int a){ // Adicionar elementos no fim da lista
        node *ptr = (node *)malloc(sizeof(node));
        ptr->data = a;

        if(){

        }else{

    }

    int main(){
        //Variáveis
        int n = 1, // Número que vai ser adicionado
        node *lista; // Lista
        while(n!=0){
            printf("Que numero adicionar?\n");
            scanf("%d", &n);
            insert(lista, n);
        }

2 answers

0

Keep in mind that you should store the reference to the first node in the list. With each insertion, you must search for the last node and then make the next node to be the new one. Also, your list started uninitialized and was not initialized by the insertion function.

typedef struct node{ // Struct para ser usada como nó...
        int data;
        struct node *next;
}node;
    node *insert(node *lista, int a){ // Adicionar elementos no fim da lista
    if(lista == NULL){
            node *aux = (node*) malloc(sizeof(node));
            aux->data = a;
            aux-> next = NULL;
            return aux;
    }
    node *ptr = (node *)malloc(sizeof(node));
    ptr->data = a;
    ptr->next = NULL;
    if(a != 0){
            node *aux = lista;
            while(aux->next != NULL){
                    aux = aux->next;
            }
            aux->next = ptr;
            return lista;
    }
    return lista;
}

int main(){
        //Variáveis
        int n = 1; // Número que vai ser adicionado
        node *lista = NULL; // Lista
        while(n!=0){
                printf("Que numero adicionar?\n");
                scanf("%d", &n);
                lista = insert(lista, n);
}
  • Dude, I don’t know if you’re using any library, but I use dev c++, it says here that on line 7 the NULL is not declared in scope

  • I have not used any library besides stdio and stdlib. I think if you replace NULL with 0 is equivalent. But NULL should work.

  • It really worked man, thank you =)

0


You can only allocate memory and create a structure like Node after the user enters some non-zero number, otherwise the program will terminate. Then just make sure it’s the first time you’re creating a Node to start the list, if that is not the case, then attach the new Node at the end of the list. This will occur until the user type zero and exit the while.

#include <stdio.h>
#include <cstring>
#include <cstdlib>

typedef struct node { // Struct para ser usada como nó...
    int data;
    struct node *next;
};

void insert(node *&lista, int numero) { // Adicionar elementos no fim da lista

    node *ptr = NULL;
    node *lista_temp = lista;

    if (numero != 0) {

        ptr = (node*)malloc(sizeof(node));
        ptr->data = numero;
        ptr->next = NULL;

        if (lista == NULL) {

            lista = ptr;
        }
        else {

            while (lista_temp->next != NULL) {

                lista_temp = lista_temp->next;
            }

            lista_temp->next = ptr;
        }
    }
}

int main() {

    //Variáveis
    int numero = 1; // Número que vai ser adicionado
    node *lista = NULL; // Lista
    node *ptr = NULL; // Ponteiro temporário

    while (numero != 0) {
        printf("Que numero adicionar?\nNumero:");
        scanf_s("%d", &numero);
        insert(lista, numero);
    }

    //Usando uma variável temporária para não perder a referência da lista
    ptr = lista;

    while (ptr->next != NULL) {

        printf("%i -> ", ptr->data);
        ptr = ptr->next;
    }

    printf("NULL\n");

    system("pause");
}
  • Lucas, using your code, I added 4 numbers but he only showed me the 3 first I guessed... which you think might be?

  • By the while clause of it, that is expected. To resolve, exchange the while for a do-while. And it gives an upvote if the answers help...

  • You’re right Ken, I didn’t realize, thank you.

Browser other questions tagged

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