Insert to end of list- efficient

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 have 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 elements, I introduced a new pointer to point to the last element and then add the new element. The program runs fine but when I ask for the main:

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

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

returns segmentation fault.

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

    
asked by anonymous 04.01.2019 / 14:58

2 answers

0

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

That said, apparently the first section of code is correct. The second is wrong: you appear to have copied the function statement and neglected to put the correct values. Try changing

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

By

Lista_r = insertLastEfi( Lista_r, nv );

Or equivalent - change the required variables. Therefore, assuming there are no more errors in other unreported points, the problem should be solved.

    
07.01.2019 / 21:39
0

It may be that in the condition

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

When you enter it, the return in case nv is null or garbage. There enters parts of the memory that could not enter and the error. For:

  

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 that   does not exist.

Also see if the first pointer you pass everything correctly, that is, pass two references.

Just another tip try using more descriptive variables

    
08.01.2019 / 23:44