Advertisement
Manioc

HT linear probing

Jun 18th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class solve {
  9.    
  10.     static class Pair implements Comparable<Pair>{
  11.         private int key;
  12.         private String value;
  13.         public Pair(int key, String value) {
  14.             this.key = key;
  15.             this.value = value;
  16.         }
  17.        
  18.         public int getKey(){ return this.key;}
  19.        
  20.         public String getValue() { return this.value;}
  21.        
  22.         public void setValue(String value) { this.value = value;}
  23.         @Override
  24.         public boolean equals(Object obj) {
  25.             if(obj != null) {
  26.                 if(obj instanceof Pair) {
  27.                     return ((Pair)obj).getKey() == this.getKey();
  28.                 }
  29.             }
  30.             return false;
  31.         }
  32.        
  33.         @Override
  34.         public String toString() {
  35.             return "<"+this.key+", "+this.value+">";
  36.         }
  37.  
  38.         @Override
  39.         public int compareTo(Pair o) {
  40.             if (this.getKey() < o.getKey()) {
  41.                 return -1;
  42.             }
  43.             if (this.getKey() > o.getKey()) {
  44.                 return 1;
  45.             }
  46.             return this.getValue().compareTo(o.getValue());
  47.         }
  48.     }
  49.     static class hashTable{
  50.        
  51.         private Pair table[];
  52.         private HashMap<Integer, String> persons;
  53.         private int sz;
  54.         private int elements;
  55.        
  56.         public hashTable(int size) {
  57.             table = new Pair[size];
  58.             persons = new HashMap<>();
  59.             sz = size;
  60.             this.elements = 0;
  61.         }
  62.        
  63.         private int getIndex(int key) {
  64.             return key%sz;
  65.         }
  66.        
  67.         private void print() {
  68.             System.out.print("[");
  69.             for(int i = 0; i < sz; i++) {
  70.                 if(table[i] != null) System.out.print(table[i].toString());
  71.                 else System.out.print("null");
  72.                 if(i != sz-1) System.out.print(", ");
  73.             }
  74.             System.out.println("]");
  75.         }
  76.        
  77.         public void put(int key, String value) {
  78.             Pair novo = new Pair(key, value);
  79.             if (this.elements < this.sz) {
  80.                     int idx = this.getIndex(key);
  81.      
  82.                     while (this.table[idx] != null) {
  83.                         if (this.table[idx].equals(novo)) {
  84.                             this.elements--;
  85.                             persons.remove(key);
  86.                             break;
  87.                         }
  88.                         idx++;
  89.                     }
  90.                    
  91.                     persons.put(key, value);
  92.                     this.table[idx] = novo;
  93.                     this.elements++;
  94.      
  95.             }
  96.             this.print();
  97.         }
  98.        
  99.         public void remove(int key) {
  100.             int idx = this.getIndex(key);
  101.             Pair aux = new Pair(key, "");
  102.             int pos = -1;
  103.             while(idx < sz) {
  104.                 if(table[idx] != null && table[idx].equals(aux)) pos = idx;
  105.                 idx++;
  106.             }
  107.            
  108.             if(pos != -1) {
  109.                 persons.remove(key);
  110.                 table[pos] = null;
  111.                 this.elements--;
  112.             }
  113.            
  114.             this.print();
  115.         }
  116.        
  117.         public void keys() {
  118.            
  119.             ArrayList aux = new ArrayList();
  120.             aux.addAll(persons.keySet());
  121.             Collections.sort(aux);
  122.             System.out.println(Arrays.toString(aux.toArray()));
  123.         }
  124.        
  125.         public void values() {
  126.             ArrayList aux = new ArrayList();
  127.             aux.addAll(persons.values());
  128.             Collections.sort(aux);
  129.             System.out.println(Arrays.toString(aux.toArray()));
  130.         }
  131.        
  132.     }
  133.     private static Scanner sc;
  134.     public static void main(String[] args) {
  135.         sc = new Scanner(System.in);
  136.        
  137.         int sz = sc.nextInt();
  138.         hashTable table = new hashTable(sz);
  139.         while(true) {
  140.             String action = sc.nextLine();
  141.             String[] choice = action.split(" ");
  142.            
  143.             if(choice[0].equals("end")) break;
  144.             switch(choice[0]) {
  145.                 case "keys":
  146.                     table.keys();
  147.                     break;
  148.                 case "remove":
  149.                     table.remove(Integer.parseInt(choice[1]));
  150.                     break;
  151.                 case "put":
  152.                     table.put(Integer.parseInt(choice[1]), choice[2]);
  153.                     break;
  154.                 case "values":
  155.                     table.values();
  156.                     break;
  157.    
  158.             }
  159.         }
  160.        
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement