I use the search 2 times in my code and it is a binary search in order to respect the criteria imposed in the heading of this exercise.
But the question is the binary search module because the compiler tells me that the arguments are being passed in the wrong way. I use ColdeBlocks and the code language is C.
I'll put the 3 modules involved, insert it in the list, remove it in the list and search binary.
Insert in 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 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;
}
}