Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DNS{
- String host;
- String IPAddr;
- DNS(String host,String IP){
- this.host=host;
- this.IPAddr=IP;
- }
- }
- class DNSHashTable{
- ArrayList<DNS> table[]; //An array of ArrayList of DNS objects
- DNSHashTable(){
- table = new ArrayList[53];
- for(int i = 0; i < table.length; i++)
- table[i] = new ArrayList<DNS>();
- }
- public static long compmap(long key){
- return key%53L;
- }
- public static long hashcodemap(String host){
- char[] data = host.toCharArray();
- long value = 0;
- int a = 33;
- int len = data.length;
- for(int i=0;i<len;i++){
- char ltr=data[(len-1)-i];
- if(Character.isLetter(ltr)){
- value *=a;
- value +=(int)ltr; //Going backwards
- }
- }
- return value;
- }
- public void insert(DNS dns){
- int index = (int)compmap(hashcodemap(dns.host));
- table[index].add(dns); //Avoids Colision using Seperate Chaining
- }
- public void Find(String hostname){
- System.out.println();
- int index = (int)compmap(hashcodemap(hostname));
- System.out.println(hostname+" May be found at table index : "+index);
- if(table[index]==null){
- System.out.println(hostname+" Index not in mapping");
- return;
- }
- ArrayList<DNS> List = table[index];
- for(int i=0;i<List.size();i++){
- if(List.get(i).host.compareTo(hostname)==0){
- System.out.println("IP Address of "+hostname+" is : "+List.get(i).IPAddr);
- return;
- }
- }
- System.out.println(hostname+" Not found in mapping index");
- }
- public static void main(String[] args) {
- DNSHashTable map= new DNSHashTable();
- map.insert(new DNS("google.com","172.217.11.174"));
- map.insert(new DNS("bbc.co.uk","212.58.241.131"));
- map.Find("google.com");
- map.Find("bbc.co.uk");
- map.Find("youtube.com");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement