Advertisement
Guest User

PAU NO CU DO JV

a guest
Nov 12th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package tabela;
  2.  
  3. public class TabelaHash {
  4.     class Elo {
  5.         private int valor;
  6.         private Elo prox = null;
  7.        
  8.         public Elo(int valor) {
  9.             this.valor = valor;
  10.         }
  11.        
  12.     }
  13.     private Elo[] elos;
  14.     private int quantidade;
  15.     public TabelaHash(int quantidade) {
  16.         this.quantidade = quantidade;
  17.         this.elos = new Elo[quantidade];
  18.     }
  19.     private int hash(int valor) {
  20.         return valor % quantidade;
  21.     }
  22.     private Elo percorrer(Elo elo,int valor) {
  23.         if( elo.valor == valor || elo.prox == null)  {
  24.             return elo;
  25.         }
  26.         return percorrer(elo.prox,valor);
  27.     }
  28.    
  29.     private void percorrerDel(Elo elo,int valor){
  30.         if(elo.prox == null) {
  31.             System.out.println("Elemento não encontrado");
  32.             return;
  33.         }
  34.         if(elo.prox.valor == valor) {
  35.             System.out.println("Elemento removido com sucesso");
  36.             elo.prox = elo.prox.prox;
  37.             return;
  38.         }
  39.         percorrerDel(elo.prox,valor);
  40.         return;
  41.     }
  42.    
  43.     public boolean buscar(int valor) {
  44.         int posicao = this.hash(valor);
  45.         if(this.elos[posicao] == null) {
  46.             return false;
  47.         }
  48.         if(this.elos[posicao].valor == valor) {
  49.             return true;
  50.         }
  51.         Elo elo = percorrer(this.elos[posicao].prox,valor);
  52.         if(elo.valor == valor) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.     public boolean insere(int valor) {
  58.         int posicao = this.hash(valor);
  59.         if(this.elos[posicao] == null) {
  60.             this.elos[posicao] = new Elo(valor);
  61.             return true;
  62.         }
  63.         if(this.elos[posicao].valor == valor) {
  64.             System.out.println("Valor já existe na tabela");
  65.             return false;
  66.         }
  67.         Elo elo = percorrer(this.elos[posicao],valor);
  68.         if(elo.valor == valor) {
  69.             System.out.println("Valor já existe na tabela");
  70.             return false;
  71.         }else {
  72.             elo.prox = new Elo(valor);
  73.             return true;
  74.         }
  75.     }
  76.    
  77.     public void remove(int valor) {
  78.         int posicao = this.hash(valor);
  79.         if(this.elos[posicao] == null) {
  80.             System.out.println("Valor não existe na tabela");
  81.             return;
  82.         }
  83.         if(this.elos[posicao].valor == valor) {
  84.             System.out.println("Elemento removido com sucesso");
  85.             this.elos[posicao] = this.elos[posicao].prox;
  86.             return;
  87.         }
  88.             this.percorrerDel(this.elos[posicao], valor);
  89.            
  90.     }
  91.    
  92.     public static void main(String[] args) {
  93.         TabelaHash tabela = new TabelaHash(3);
  94.         for(int i=0;i<10;i++) {
  95.             tabela.insere(i);
  96.         }
  97.         for(int i=0;i<10;i++) {
  98.             int valor = (int) (Math.random() * 11);
  99.             tabela.remove(valor);
  100.         }
  101.        
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement