|
//TablaHash.java public class TablaHash { private int m; //tamaño de la tablaHash.
private NodoHash[] e; //NodoHash
public TablaHash(int m) //Constructor
{ this.m=m; e = new NodoHash[m];
for (int i=0; i< m; i++) e[i] = new NodoHash(); } private int getHash(int newElement)//Función Hash { return newElement % m; }
public void addElemento(int newElement, String nom) {//Agrega un elemento int index = getHash(newElement); e[index].insertarNodo(newElement, nom); }
public NodoHash getElemento(int key) { //Obtiene elemento int index = getHash(key); NodoHash item = e[index].locateNodo(key); return item;
}
public int getPos(int key){ int pos= getHash(key); return pos; }
public void removeElemento(int key) { //Obtiene elemento
int index=getHash(key); NodoHash item=e[index].locateNodo(key); item.key=0;
}
} //NodoHash.java
public class NodoHash{
int key;//Llave. String nombre;//Información extra. NodoHash nextNode;
public NodoHash() {
key =0; nombre=null; }
public NodoHash(NodoHash x){
key = x.key; nombre = x.nombre; }
public NodoHash(int key, String str){
this.key = key; nombre = str; }
public void insertarNodo(int key, String nombre) {
insertarNodo(key, nombre, this);
} public void insertarNodo(int key, String nombre, NodoHash actual){
if(actual.getNextNode() == null)
actual.setNextNode(new NodoHash(key, nombre)); else insertarNodo(key, nombre, actual);
}
public NodoHash getNodo(int key) { return getNodo(key, this); }
public NodoHash getNodo(int key, NodoHash actual) {
if (key == actual.getKey()) return actual;
else if (actual.getNextNode()== null) return null;
else return getNodo(key, actual.getNextNode()); }
public int getKey() { return key; }
public String getNombre(){ return nombre; }
public NodoHash getNextNode() { return nextNode; }
public void setNextNode(NodoHash nextNode) { this.nextNode = nextNode; }
public String toString() { return "Llave " + key + " Nombre " + nombre; } }
//CLASE QUE VERIFICA EL FUNCIONAMIENTO //Manager.java class Manager{
public static void main(String arg[]) {
TablaHash tabla = new TablaHash(10); tabla.addElemento(1, "Martin"); tabla.addElemento(6, "Julio"); tabla.addElemento(3, "Jajaja");
tabla.addElemento(7, "Jorge");
tabla.removeElemento(3);
//Eliminar todos. //RemoveNodo. //BuscarNodo. //InsertarNodo.
for (int i=1; i<10; i++)
{ System.out.println (tabla.getElemento(i));
}
} }
|