Package lec09.glab.searchsort

Source Code of lec09.glab.searchsort.HashTable

package lec09.glab.searchsort;

import lec09.glab.structs.ListNode;

public class HashTable {

 
  private ListNode[] nodVals;

  public HashTable(int nBuckets) {
 
   
    nodVals = new ListNode[nBuckets];
       
   
  }
  //O(1) very fast
  public String contains(Object obj){
   
    String strR = "";
    int nCount = 0;
   
    int nHash = obj.hashCode();
    nHash = Math.abs(nHash);
    nHash = nHash % nodVals.length;
   
    ListNode nodCurrent = nodVals[nHash];
    //nCount++;
    while(nodCurrent != null){
      nCount++;
      if(nodCurrent.getValue().equals(obj)){
        strR += obj + " found at " + nHash + " : ";
        break;
      }
     
      nodCurrent = nodCurrent.getNext();
    }
   
    strR +=  nCount + " iterations.";
    return strR;
   
  }
 
 
  public void add(Object obj){

    int nHash = obj.hashCode();
    nHash = Math.abs(nHash);
    nHash = nHash % nodVals.length;
   
    ListNode nodCurrent = nodVals[nHash];

    if(nodCurrent == null){
      nodVals[nHash] = new ListNode(obj, null);
    }
    else {
     
      while(nodCurrent.getNext() != null)
        nodCurrent = nodCurrent.getNext();
 
      nodCurrent.setNext(new ListNode(obj, null));
    }

  }
 
 
 
 
}
TOP

Related Classes of lec09.glab.searchsort.HashTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.