Home > Net >  Root node getting lost when i add elements to it?
Root node getting lost when i add elements to it?

Time:09-25

So i declare a pointer to a struct and set it equals to NULL,

struct nodo * root = NULL;

then in a function i created i add some given values to said pointer.

void add(struct nodo * root, int id, int age){
if (root== NULL){
    root= (struct nodo *)malloc(sizeof(struct nodo));
    root->id = id;
    root->age= age;
    root->prox = NULL;
}

When i check for the values i have given to the pointer in the fucntion using printf, i see that in fact, theyre in the struct pointed by the root, but after i have called the fucntion, if i check if the values are still there it returns nothing. for example (in main):

            add(raiz_idoso, id, age);
            printf("%d\n", root_idoso->id);
            printf("%d\n", root)idoso->age); // this returns nothing! but if i did the same thing inside the function add, it would return the values of id and age 

Could anyone help me understand what it is i am doing wrong please?

Here is the complete code if its of any help, i have translated some things in the previous part for it to be easier to understand, its in portuguese:

#include <stdio.h>
#include <stdlib.h>

struct nodo {
    int id;
    int idade;
    struct nodo * prox;
};


void adicionar (struct nodo * raiz, int id, int idade){
    if (raiz == NULL){
        printf("oi");
        raiz = (struct nodo *)malloc(sizeof(struct nodo));
        raiz->id = id;
        raiz->idade = idade;
        raiz->prox = NULL;
    }
    else if (raiz -> prox == NULL){
        struct nodo * temp;
        raiz->prox = (struct nodo *)malloc(sizeof(struct nodo));
        temp = raiz->prox;
        temp->id = id;
        temp->idade = idade;
        temp->prox = NULL;
    }
    else{
        adicionar(raiz->prox, id, idade);
    }
}
                                                                    
void remover (struct nodo * raiz){
    if (raiz != NULL){
    raiz = raiz->prox;
    }
}


void imprimir (struct nodo * raiz, int primero){
    if (raiz == NULL && primero == 1){
        printf("fila vazia!\n");
    }
    else if (raiz != NULL){
        printf("ID: %d IDADE: %d\n", raiz->id, raiz->idade);
        imprimir(raiz->prox, 0);
    }
}

int main (void){
    char entrada;
    int id, idade, prioridade, counter;
    struct nodo * raiz_idoso = NULL;
    struct nodo * raiz_nidoso = NULL;
    scanf("%d", &prioridade);

    counter = 0;

    while(entrada != 'f'){
        scanf(" %c", &entrada);
        if (entrada == 'a'){ 
            scanf(" %d", &id);
            scanf(" %d", &idade);
            if (idade > 60){
                adicionar(raiz_idoso, id, idade);
                printf("%d\n", raiz_idoso->id);
                printf("%d\n", raiz_idoso->idade);
            }
            else if (idade < 60){
                adicionar(raiz_nidoso, id, idade);
            }
            
        }
        else if (entrada == 'r'){
            if (raiz_idoso == NULL && raiz_nidoso == NULL){

            }
            else if (raiz_idoso == NULL){
                counter = 0;
                remover(raiz_nidoso);
            }
            else if (raiz_nidoso == NULL){
                counter = 0;
                remover(raiz_idoso);
            }
            else{
                if (counter > prioridade){
                    counter = 0;
                    remover(raiz_nidoso);

                }
                else{
                    counter  = 1;
                    remover(raiz_idoso);
                }
            }
        }
        else if (entrada == 'i'){
            printf("fila de idosos:\n");
            imprimir(raiz_idoso, 1);
            printf("fila de nao-idosos:\n");
            imprimir(raiz_nidoso, 1);
            printf("----------\n");
        }
    }
}

CodePudding user response:

Your add function declares root as a parameter. This means that, within the add function, root is a local variable, and setting it has no effect on the global definition of root.

If you want it to operate directly on the global definition of root, just remove the root parameter:

void add(int id, int age) {
   ...
}

That way it will operate directly on the global definition of root.

Alternatively, if you don't want to hard-wire it to a single global definition of root, you can solve it in one of two ways. One is to return the new root value:

struct nodo *add(struct nodo *root, int id, int age) {
    ...
    return root;
}

In this case the caller would need to assign the return value to the global root (or any other tree root you desire).

An alternate way to achieve the same effect it to pass a pointer to the root pointer:

void add(struct nodo **root_ptr, int id, int age) {
    ...
    *root_ptr = ...
    ...
}

For this version, the caller would pass the address of the root pointer, rather than the root pointer itself.

  • Related