/** *
 *  HashTable.java
 *
 *  Revisions: 1.0 Oct. 06, 2002
 *               Created the HashTable class
 *			   1.1 Oct. 10, 2002
 *				 Compiled, finished, Commented
 *
 *  
* * Collaboration Statement: * I worked on the homework assignment alone, using only * course materials. * * Created with JCreatorLE, some indents are off when viewed through notepad * or EMACS * * @author Jose Manuel Caban * @version Version 1.0, Oct. 06, 2002 */ typedef struct HashNode { HashNode* next; int key; int data; }; HashNode[] hTable; //////////////// //Constructors// //////////////// public HashTable(int size) { hTable = new HashNode[size]; } /////////// //Methods// /////////// /** *add data to a given location *@param data added to location key *@param key, the location to store data */ public void add(Object key, Object data) { int iKey = Math.abs(key.hashCode()) % hTable.length; if(hTable[iKey] == null) { hTable[iKey] = new HashNode(key,data); } else { HashNode temp = new HashNode(key,data); temp.setNext(hTable[iKey]); hTable[iKey] = temp; } } /** *@return the data located *@param key */ public Object get(Object key) { int iKey = Math.abs(key.hashCode()) % hTable.length; if(hTable[iKey] == null) { return null; } HashNode current = hTable[iKey]; while(current != null) { if(current.getKey().equals(key)) { return current.getData(); } current = current.getNext(); } return null; }//end get(@key) /** *Remove hashNode at key and return data *@param key, the node to be removed */ public Object remove(Object key) { int iKey = Math.abs(key.hashCode()) % hTable.length; if(hTable[iKey] == null) { return null; } HashNode current = hTable[iKey]; if(current.getKey().equals(key)) { hTable[iKey] = current.getNext(); return current.getData(); } else { //if(the list is long and annoying) //set predefs HashNode hPrev = current; //a little longer and its hPrevInstance current = current.getNext(); while(current != null) { if(current.getKey().equals(key)) { hPrev.setNext(current.getNext()); return current.getData(); } current = current.getNext(); hPrev = hPrev.getNext(); } return null; } }//end remove(@key) /** *@return enumeration of of HashTable */ public Enumeration elements() { Vector vTemp = new Vector(); HashNode hCurr; for(int i=0; i
* @param args a String array of command line arguments. */ public static void main(String[] args) { HashTable hashey = new HashTable(5); //test add hashey.addRandom(15); System.out.println(hashey); hashey = new HashTable(5); hashey.addRandom(12); System.out.println("Testing elements"); Enumeration e = hashey.elements(); while(e.hasMoreElements()) { System.out.println(e.nextElement()); } //hehehehehehhehehe, lets test this sucker for REAL //don't try this at home :) //approx. runtime on a Tiger MPX dual MP2100+ 1.5GB RAM //thats 100% CPU utilaztion at 1.733GHz IPC3 //around 60 seconds (im guessing) //:) hashey = new HashTable(200); hashey.addRandom(2000); System.out.println(hashey); }// end of main(String[] args) }// end of class HashTable