Inserir elementos repetidos de Tabela Hash em Lista Encadeada

0 respostas
javaprogramação
Luan_Magalhaes

Criei uma estrutura de Registro e Tabela Hash e gostaria de inserir os elementos com registro repedido em uma lista encadeada. Pensei em criar um if, utilizar meu método de buscaElemento, e a própria classe de LinkedList do Java porém não sei muito bem como implementar.

package hash;

public class Registro {

private Registro proximo;
private int chave;
private int valor;
private int count = 0;

public Registro() {
    this.proximo = null;
}

public Registro(int key, int value) {
    this.chave = key;
    this.valor = value;
    count++;
}

public Registro getProximo() {
    return proximo;
}

public void setProximo(Registro proximo) {
    this.proximo = proximo;
}

public int getChave() {
    return chave;
}

public void setChave(int chave) {
    this.chave = chave;
}

public int getValor() {
    return valor;
}

public int getcount() {
    return count;
}

public void setValor(int valor) {
    this.valor = valor;
}

@Override
public int hashCode() {
    return chave % 100;
}

}

package hash;

public class TabelaHash {

private Registro[] valores;

public TabelaHash() {
    valores = new Registro[100000];
}

public void insere(Registro registro) {
    int posicao = registro.hashCode();

    if (valores[posicao] == null) {
        valores[posicao] = registro;        // posição vazia, novo registro
    } else {
        Registro reg = valores[posicao];

        if (reg.getChave() == registro.getChave()) // primeiro elemento
        {
            reg.setValor(registro.getValor());      // atualiza valor

            return;
        }

        while (reg.getProximo() != null) // meio da lista
        {
            if (reg.getChave() == registro.getChave()) // análogo
            {
                reg.setValor(registro.getValor());

                return;
            }

            reg = reg.getProximo();
        }

        if (reg.getChave() == registro.getChave()) // último elemento
        {
            reg.setValor(registro.getValor());

            return;
        }

        reg.setProximo(registro); // novo registro vai para a última posição
    }
}

public Registro buscarChave(int chave) {
    Registro r = new Registro();

    r.setChave(chave);

    int posicao = r.hashCode();

    Registro resultado = valores[posicao];

    if (resultado != null && resultado.getChave() == chave) {
        return resultado;
    } else {
        while (resultado != null) {
            resultado = resultado.getProximo();

            if (resultado != null && resultado.getChave() == chave) // busca
            {
                return resultado; // 
            }
        }
    }

    return null;    // retorna null caso não encontre
}

public Registro buscarElemento(int elemento) {
    Registro r = new Registro();

    r.setValor(elemento);

    int posicao = r.hashCode();

    Registro resultado = valores[posicao];

    if (resultado != null && resultado.getValor() == elemento) {
        return resultado;
    } else {
        while (resultado != null) {
            resultado = resultado.getProximo();

            if (resultado != null && resultado.getValor() == elemento) // busca
            {
                return resultado; // 
            }
        }
    }

    return null;    // retorna null caso não encontre
}

}

Criado 6 de junho de 2019
Respostas 0
Participantes 1