Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class ArrayList{
  2.     Integer []list;
  3.     int []ri;
  4.     int length;
  5.     public ArrayList(){
  6.         list=new Integer[10];
  7.         length=0;
  8.     }
  9.  
  10.     public ArrayList(int size){
  11.         list=new Integer[size];
  12.         length=0;
  13.     }
  14.  
  15.     public void insertLP(int item){//insertion for linear probing
  16.         if(length!=list.length){
  17.             linearProbing(item);
  18.         }
  19.     }
  20.     public void insertRP(int item){//insertion for random probing
  21.         if(length==0){
  22.             ri=randomPermutation();
  23.         }
  24.         if(length!=list.length){
  25.             randomProbing(item);
  26.         }
  27.     }
  28.     public void insertQP(int item){//insertion for quadratic probing
  29.         if(length!=list.length){
  30.             quadraticProbing(item);
  31.         }
  32.     }
  33.  
  34.     public void linearProbing(int item){
  35.         int index=modulo(item);
  36.         boolean found=false;
  37.         while(list[index]!=null){
  38.             if(list[index]==item){
  39.                 found=true;
  40.                 break;
  41.             }
  42.             else{
  43.                 index=(index+1)%list.length;
  44.             }
  45.         }
  46.         if(found){
  47.             System.err.println("Item Already Exist");
  48.         }
  49.         else{
  50.             list[index]=item;
  51.             length++;
  52.         }
  53.     }
  54.  
  55.     public void randomProbing(int item){
  56.         int index=modulo(item);
  57.         boolean found=false;
  58.         int i=0;
  59.         int t=index;
  60.         while(list[index]!=null){
  61.             if(list[index]==item){
  62.                 found=true;
  63.                 break;
  64.             }
  65.             else{
  66.                 index=(t+ri[i])%list.length;
  67.             }
  68.             i++;
  69.         }
  70.         if(found){
  71.             System.err.println("Item Already Exist");
  72.         }
  73.         else{
  74.             list[index]=item;
  75.             length++;
  76.         }
  77.     }
  78.     public void quadraticProbing(int item){
  79.         int index=modulo(item);
  80.         boolean found=false;
  81.         int t=index;
  82.         int i=0;
  83.         while(list[index]!=null){
  84.             if(list[index]==item){
  85.                 found=true;
  86.                 break;
  87.             }
  88.             else{
  89.                 index=(int)(t+Math.pow(i,2))%list.length;
  90.             }
  91.             i++;
  92.         }
  93.         if(found){
  94.             System.err.println("Item Already Exist");
  95.         }
  96.         else{
  97.             list[index]=item;
  98.             length++;
  99.         }
  100.     }
  101.  
  102.     public int modulo(int item){//get initial hash index
  103.         return item%list.length;
  104.     }
  105.  
  106.     public int []randomPermutation(){//random permutation of the numbers 1 to size-1(u can create ur own algorithm)
  107.         int size=list.length-1;
  108.         int []index=new int[size];
  109.         for(int i=0;i<index.length;i++){
  110.             index[i]=i+1;
  111.         }
  112.         for(int i=0;i<index.length;i++){
  113.             int j=(int)Math.round(Math.random()*(size-1));
  114.             int temp=index[j];
  115.             index[j]=index[i];
  116.             index[i]=temp;
  117.         }
  118.         return index;
  119.     }
  120.  
  121.     public String toString(){  
  122.         String out=list[0]+"";
  123.         for(int i=1;i<list.length;i++){
  124.             out+=","+list[i];
  125.         }
  126.         return out;
  127.     }
  128.  
  129. }