The queue code has an error in the search. How can I fix it?

Asked

Viewed 38 times

0

I use the search twice in my code and it is a binary search in order to respect the criteria imposed in the header of this exercise.

But the doubt lies in the binary search module because the compiler tells me that the arguments are being passed wrong. Coldeblocks usage and code language is C.

I will put the 3 modules involved, insert in the list, remove in the list and binary search.

Queue:

int inserirNaLista(int ***l, int n, int x)
{
        int i, M;
        M=elementosNaLista(&l,n);
        if(M<n){
            if(buscaBinaria(&l, n,x)==-1){
                l[M]=(int *)malloc(sizeof(int));
                *l[M]=x;
                return 1;
            }

        else{
           return 0; //Elemento já existte
        }
     }else{
        return -1; //Overflow
     }
}

Remove in the queue:

int removerNalista(int **l, int n, int x)
{
     int i, indice, M;
     M=elementosNalista(&l,n);
     if(M==0)
        return-1;// Underflow
        else{
            indice= buscaBinaria(&l, n, x);
            if(indice==-1)
                return 0;//Elemento não existe
                else{
                    if(indice== (n-1))
                    {
                        free(l[indice]);
                        l[indice]=NULL;
                    }else{
                            free(l[indice]);
                            for(i=indice; i<M; i= i+1) l[i]= l[i+1];
                            l[M-1]= NULL;
                    }
                    return 1;
                }
        }
}

Binary search:

int buscaBinaria(int l, int n, int x)
{
    int inf,sup, meio,m,busca;
    inf=0;
    sup=(n-1);

    while(inf<=sup)
    {
        meio=((inf+sup)/2);

        if(l[meio] == x)
        {
            busca=meio;
            return busca;

        }else
        {
            if(l[meio]>x)
            {
                sup=meio-1;
            }else
            {
                inf=(meio+1);
            }
        }
    busca=-1;
    }

}

1 answer

0


In function buscaBinaria, your first parameter is an integer (int l) and you try to use it as an array in if(l[meio] == x).

Browser other questions tagged

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