Insert at the end of efficient-list

Asked

Viewed 58 times

0

typedef struct NODO{
    int custo;
    int linha;
    int coluna;
    struct NODO *nseg[2];
}Nodo;

 Nodo * insertLastEfi(Nodo *L,Nodo *nv){
    Nodo * aux =L;
    if (L==NULL) {
        return nv;
    }
    while (L->nseg[1]!=NULL) {
        L=L->nseg[1];
    }
    L->nseg[1]=nv->nseg[1];
    L->nseg[0]=nv->nseg[0];
    return aux;
}

I implemented the following function mentioned above. What I want to do is:

to insert a new element in the list and instead of going through all the elements, I introduced a new pointer to point to the last element and then add the new element. The program goes well but when I ask in the main:

Lista_r=insertLastEfi(Nodo *L,Nodo *nv)

printf("%d\n",Lista_r->custo);

returns Segmentation fault.

The function insertLastEfi is called 100 times to copy 100 values to list nodes.

2 answers

0

It may be that in the condition

if (L==NULL){
    return nv;
}

When it enters it, the return that in the case nv is void or with garbage. There enters into parts of memory that could not enter and error. Because:

Segmentation failure (Segmentation fault, also referred to by segfault) is an error that occurs in program when it tries to access (for reading or writing) an address in the RAM memory that is reserved for another program (or the operating system itself) or which there is no.

Also see if on the first pointer Voce passes everything right, IE, passes two references.

Just one more tip try to use more decryptive variables

0


It is difficult to know exactly if there is any other problem with a fraction of the code. Always provide the code in the most complete way possible since without it we will not be able to test. Provide the input files and everything relevant. I must also say that the error may be in another area that you do not think.

That said, apparently the first section of code is correct. The second one is wrong: you seem to have copied the function statement and forgotten to put the correct values. Try switching

Lista_r=insertLastEfi(Nodo *L,Nodo *nv)

For

Lista_r = insertLastEfi( Lista_r, nv );

Or equivalent - change the required variables. With this, assuming that there are no more errors in other points not informed, the problem must be solved.

Browser other questions tagged

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