【问题标题】:Root node getting lost when i add elements to it?当我向它添加元素时,根节点会丢失吗?
【发布时间】:2023-03-25 13:01:01
【问题描述】:

所以我声明了一个指向结构的指针并将其设置为NULL,

struct nodo * root = NULL;

然后在我创建的函数中,我将一些给定的值添加到所述指针。

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;
}

当我使用 printf 检查我在函数中赋予指针的值时,我看到事实上,它们在根指向的结构中,但是在我调用函数之后,如果我检查值是否还在那里它什么都没有返回。例如(主要):

            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 

谁能帮我理解我做错了什么?

如果有帮助的话,这里是完整的代码,为了更容易理解,我已经翻译了上一部分的一些东西,它是葡萄牙语:

#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");
        }
    }
}

【问题讨论】:

    标签: c pointers struct malloc


    【解决方案1】:

    您的add 函数将root 声明为参数。这意味着,在add函数中,root是一个局部变量,设置它对root的全局定义没有影响。

    如果你想让它直接对root的全局定义进行操作,只需去掉root参数即可:

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

    这样它将直接对root的全局定义进行操作。

    或者,如果您不想将其硬连接到 root 的单个全局定义,您可以通过以下两种方式之一来解决它。一种是返回新的根值:

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

    在这种情况下,调用者需要将返回值分配给全局 root(或您想要的任何其他树根)。

    实现相同效果的另一种方法是将指针传递给根指针:

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

    对于这个版本,调用者将传递根指针的地址,而不是根指针本身。

    【讨论】:

    • 我明白了,非常感谢您的帮助!编辑:当我从 add 函数中删除 root 参数时,它说 root 是未定义的,即使我使用了我实际使用的正确变量名(“raiz_idoso”)你的意思是我必须将 root 声明为全局?跨度>
    • @JOAOVITORPEREIRAVENTURA 如果您使用这种方法,那么可以。我可能误解了您帖子的第一节摘录,我认为这应该是一项全球宣言。如果它是本地的,那么其他两种方法之一将起作用。在这种情况下,仅返回新根可能是最简单的。第三种方法,即传递根指针的地址,当您想使用其他东西作为返回值,或者尽量减少忘记更新根的机会(或者如果您想要更紧凑的函数调用)时,这种方法很有用。
    • 是的,它是本地的,谢谢!我让它工作了
    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多