Advertisement
Guest User

Lab 7

a guest
Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. class DNS{
  2.         String host;
  3.         String IPAddr;
  4.         DNS(String host,String IP){
  5.  
  6.                 this.host=host;
  7.                 this.IPAddr=IP;
  8.         }
  9. }
  10. class DNSHashTable{
  11.         ArrayList<DNS> table[]; //An array of ArrayList of DNS objects
  12.         DNSHashTable(){
  13.         table = new ArrayList[53];
  14.         for(int i = 0; i < table.length; i++)
  15.                 table[i] = new ArrayList<DNS>();
  16.         }
  17.         public static long compmap(long key){
  18.                 return key%53L;
  19.         }
  20.         public static long hashcodemap(String host){
  21.                 char[] data = host.toCharArray();
  22.                 long value = 0;
  23.                 int a = 33;
  24.                 int len = data.length;
  25.                 for(int i=0;i<len;i++){
  26.                         char ltr=data[(len-1)-i];
  27.                         if(Character.isLetter(ltr)){
  28.                                 value *=a;
  29.                                 value +=(int)ltr; //Going backwards
  30.                         }
  31.                 }
  32.                 return value;
  33.         }
  34.         public void insert(DNS dns){
  35.                 int index = (int)compmap(hashcodemap(dns.host));
  36.                 table[index].add(dns); //Avoids Colision using Seperate Chaining
  37.         }
  38.         public void Find(String hostname){
  39.                 System.out.println();
  40.                 int index = (int)compmap(hashcodemap(hostname));
  41.                 System.out.println(hostname+" May be found at table index : "+index);
  42.                 if(table[index]==null){
  43.                         System.out.println(hostname+" Index not in mapping");
  44.                         return;
  45.                 }
  46.                 ArrayList<DNS> List = table[index];
  47.                 for(int i=0;i<List.size();i++){
  48.  
  49.                         if(List.get(i).host.compareTo(hostname)==0){
  50.                                 System.out.println("IP Address of "+hostname+" is : "+List.get(i).IPAddr);
  51.                                 return;
  52.                         }
  53.                 }
  54.                 System.out.println(hostname+" Not found in mapping index");
  55.         }
  56.         public static void main(String[] args) {
  57.                 DNSHashTable map= new DNSHashTable();
  58.                 map.insert(new DNS("google.com","172.217.11.174"));
  59.                 map.insert(new DNS("bbc.co.uk","212.58.241.131"));
  60.                 map.Find("google.com");
  61.                 map.Find("bbc.co.uk");
  62.                 map.Find("youtube.com");
  63.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement