/** *
* 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