Пример #1
0
HASH* insere(HASH *hash, int numero, int* tamanho, int *numDeElementos){
	int indice;
	indice = funcaoHash(numero, *tamanho);
	HASH auxiliar = hash[indice]; //auxiliar recebe a posição do vetor que deve ser inserido o número
	/*Se 'auxiliar' não é NULL signica que tem números inseridos nessa posição, entao ele precisa verificar que não está
	 * inserindo um número que já existe, para isso ele precisa percorrer até o fim ou até encontrar o número igual.*/
	while (auxiliar != NULL ){
		if ( auxiliar->num == numero ){
			return hash; //Significa que tá inserindo um número duplicado
		}
		auxiliar= auxiliar->prox;
	}
	if (auxiliar == NULL){ 	// Se tentar inserir um número duplicado, não entrará aqui
		auxiliar = (HASH)malloc(sizeof(HASH)); //cria uma posição pra inserir o número 
		if (auxiliar == NULL){ 		//Tratamento de memória
			return NULL;
		}
		auxiliar->num= numero;
		auxiliar->prox= hash[indice];//Se a hash[indice] estiver vazia, auxiliar recebe NULL
		hash[indice]=auxiliar;
		(*numDeElementos)++; 
	}
	if (((float)*numDeElementos / *tamanho)>=0.6){
		hash= rehash(hash, *(&tamanho)); //Faz rehash caso o fator de carga seja >= que 0.6
	}
	return hash;
}
Пример #2
0
//obtem um Elemento da Hash dada sua chave
Elemento * obtemDaHash(Hash* h, Chave c,int boolSBB ){
	int pos = funcaoHash(c, h);
	Elemento *x;

	if (boolSBB==1){
		x = pesquisaSBB(h->hashSBB, &c);
	} else{
		x = pesquisa(h->hash, &c);
	}
	return x;
}
Пример #3
0
//insere um Elemento na Hash
void insereNaHash(Hash* h, Elemento* x, int boolSBB){

	int pos = funcaoHash(x, h);

	if (boolSBB==1){
		if (h->hashSBB[pos]==NULL){
				h->hashSBB[pos] = criaArvoreSBB(x);
		} else{
			insereElementoSBB(&(h->hashSBB[pos]), x);
		}
		h->nElem++;
	}else{
		if (h->hash[pos]==NULL){
			h->hash[pos] = criaArvore(x);
		} else{
			insereElemento(h->hash[pos], x);
		}
		h->nElem++;
	}
}
Пример #4
0
int removeNumero (HASH *hash, int numero, int tamanho, int *numDeElementos){
	int indice;
	indice=funcaoHash(numero, tamanho);
	HASH auxiliar = hash[indice];
	if (auxiliar == NULL ){
		return -1; //Número não existe
	}
	else{
		if ( auxiliar->num == numero){
			if(auxiliar->prox != NULL){   //Deseja excluir o primeiro elemento da lista
				hash[indice]=auxiliar->prox;
				free(auxiliar);
				(*numDeElementos)--;
			}
			else {	//Auxiliar->prox == NULL, significa que era o único elemento da lista encadeada
				hash[indice]=NULL;
				free(auxiliar); //Não sei se essa linha é necessária*********************
				(*numDeElementos)--;
			}		
		}
		else{
			HASH anterior=auxiliar;
			auxiliar=auxiliar->prox;
			while (auxiliar->num != numero ){
				anterior=auxiliar;
				auxiliar=auxiliar->prox;
				if (auxiliar==NULL){
					return -1;
				}
			}
			if (auxiliar->num == numero ){ // O número existe 
				anterior->prox=auxiliar->prox; //Deseja-se excluir o auxiliar
				free (auxiliar);
				(*numDeElementos)--;
			}
		}
	}
	return 1;
}