Advertisement
LeLeTe

EP Hashing

Nov 10th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. /** Hash Function: EP da disciplina ACH2002. Data de entrega: 27.11.2015
  2.   * Andrei Oliveira, nusp 9004504.
  3. */
  4. public class Hash extends HashTable {
  5.    
  6.     Hash(int nbin, HashCalc h)
  7.     {
  8.         super(nbin, h);
  9.     }
  10.  
  11.     public void insere(String chave, String conteudo) {
  12.         int pos = h.calc(chave);
  13.         if(!bin[pos].isEmpty()) colisoes[pos]++; // Será que toda a lista vazia tem colisão?
  14.         elementos[pos]++;
  15.         bin[pos].insereNoFinal(chave, conteudo);
  16.     }
  17.  
  18.     public String busca(String chave) {
  19.         int pos = h.calc(chave);
  20.         if(bin[pos].isEmpty()) return null;
  21.         String ret = bin[pos].busca(chave);
  22.         if(ret != null) return ret;
  23.         return null;
  24.     }
  25.    
  26.     public void remove(String chave) {
  27.         int pos = h.calc(chave);
  28.         if(!bin[pos].remove(chave))
  29.             System.out.println("Não foi removido;");
  30.         else
  31.         {
  32.             elementos[pos]--;
  33.             System.out.println("Removido");
  34.         }
  35.     }
  36.  
  37.     public void imprime() {
  38.         for(int i = 0; i < bin.length; i++)
  39.         {
  40.             bin[i].imprime();
  41.         }
  42.         System.out.println("Todas as listas impressas;");
  43.     }
  44.  
  45.     public void resumo() {
  46.         for(int i = 0; i < colisoes.length; i++)
  47.         {
  48.             System.out.println("Lista " + i + ": " + colisoes[i] + " colisões e " + elementos[i] + " elementos;");
  49.         }
  50.        
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement