Error creating child node, generalized list

Asked

Viewed 86 times

0

The error is found in else of the method criarFilho, I was able to locate it but I can not find solution for it.

The debug Dev-C++ does not point out anything.

    #include <stdio.h>
    #include <stdlib.h>
    //  #include "decisor.h"


    typedef struct node {
        int categoria;
        int atributoOuDecisao;
        struct node *prox;
        struct node *lista;
    } No;

    No *criaArvore(void){

    No *inicio = (No*)malloc(sizeof(No));                       //Aloca memória para filho.
        inicio->atributoOuDecisao = 0;
        inicio->categoria = 99;
        inicio->lista = NULL;                                       //Inicializa Variaveis
        inicio->prox = NULL;
        return inicio;                                              //Retorna Raiz
    }

    No *criaFilho (No *pai, int atributoDoPai, int categoriaDoFilho, int atributoOuDecisao){

        No *p1 = (No*)malloc(sizeof(No));                           //Cria Nó Filho
        p1->atributoOuDecisao = atributoOuDecisao;
        p1->categoria = categoriaDoFilho;
        p1->lista = NULL;
        p1->prox = NULL;
        if (pai->lista = NULL) {                                    //Testa se sublista é vazia, se sim então inicializa sublista com Filho.
            pai->lista = p1;
        }
        else {                                                      //Se pai possui sublista, percorre sublista até o fim com auxiliar e posiciona Filho na ultima posição.
            No *aux;                                            
            aux = pai->lista;
            while (aux->prox != NULL){
                aux = aux->prox;
            }
            aux->prox = p1;
        }
        return p1;                                                  //Retorna      ponteiro para nó Filho
    }


    int main () {
        No *aux, *aux2, *arv;
        int *v;
        arv=criaArvore();
        aux=criaFilho(arv, 1, 1, 3);
        criaFilho(aux, 3, 0, 1);
        return 0;
    }

1 answer

1


Lacked a = in the if. You assigned a value instead of comparing. Note that I improved some aspects of the code, as I had done before. In C there are no methods, there are functions. I also advise using another compiler/IDE. This one has problems and it doesn’t help to find simple errors like this. There’s something more modern.

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

typedef struct node {
    int categoria;
    int atributoOuDecisao;
    struct node *prox;
    struct node *lista;
} No;

No *criaArvore(void) {
    No *inicio = malloc(sizeof(No));
    inicio->atributoOuDecisao = 0;
    inicio->categoria = 99;
    inicio->lista = NULL;
    inicio->prox = NULL;
    return inicio;
}

No *criaFilho(No *pai, int atributoDoPai, int categoriaDoFilho, int atributoOuDecisao) {
    No *p1 = malloc(sizeof(No));
    p1->atributoOuDecisao = atributoOuDecisao;
    p1->categoria = categoriaDoFilho;
    p1->lista = NULL;
    p1->prox = NULL;
    if (pai->lista == NULL) { // <================== erro aqui
        pai->lista = p1;
    } else {
        No *aux = pai->lista;
        while (aux->prox != NULL) {
            aux = aux->prox;
        }
        pai->prox = p1;
    }
    return p1;
}

int main() {
    No *arv = criaArvore();
    No *aux = criaFilho(arv, 1, 1, 3);
    criaFilho(aux, 3, 0, 1);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Again, thank you very much. Recommends some IDE in particular?

  • @Marcospinheiromoura again read the [tour] to understand how the site works. Visual Studio is the most used in Windows. If you prefer to use the GCC gives in VS too, but it is not common, there is a lot of option. There are those who like Code::Blocks, others prefer other things. There is a lot of offer of good things. That is, basically this is the only bad thing. I can’t understand why so many people choose to use a software with problems, that if search or show easy

  • @Marcospinheiromoura you can accept in the other too.

Browser other questions tagged

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