2
I am implementing a hash table with quadratic scanning method. My question is about the position 0 of the vector used for table implementation. Follow the code below:
public boolean inserir(Pessoa item) {
int valorHash = hash(item.getCelular());
int contador = 1;
int inicialHash = valorHash;
while(bancoDados[valorHash] != null && !(bancoDados[valorHash].getCelular().equals(-1))) {
valorHash += (contador*contador);
contador++;
valorHash %= tamanhoMax;
}
bancoDados[valorHash] = item;
return true;
}
Through this method the 0 position will never be found for an insertion. I must ignore this position in the vector and work with the rest or is there some way to make my Hash value (valorHash
) reach position 0?
NOTE: I use a person’s cell number to generate the valorHash
in another method. When a person is removed from my table I replace them with another person, only with an invalid cell number (-1), to indicate that that position is available.
I’m probably missing a bit of theoretical background, but I don’t understand why a hash table would want to store an element in a position other than the hash itself, after all, how is one going to find the data reliably afterwards? I also didn’t understand the phone with
-1
, why not simply assignnull
? What’s more, I believe you have no problem leaving the first element of the vector unused, but if you want to make use of it, just move the result by subtracting 1 from the hash when storing and when retrieving from the vector.– utluiz