Zum Inhalt
Benutzer:Riemer
Aus ProgrammingWiki
Archivierte Version vom 2012-03-28T15:19:07+00:00.
Aktuelle Version
Klassen-Definitionen
public Object Link(int d) { int data = d; Object next; public int getKey() { return data; } public void displayLink() { System.out.print(data + " "); } return this; } public Object List() { Object first = null; Object last = null; int steps = 0; public void insert(Object theLink) { steps = 0; System.out.print("Einfügen von: "); theLink.displayLink(); if (first == null) { steps++; first = theLink; last = first; } else { steps++; last.next = theLink; last = last.next; } System.out.println("Schritte: " + steps); } public void delete(int key) { Object previous = null; Object current = first; steps = 0; while (current != null && key != current.getKey()) { steps++; previous = current; current = current.next; } if (previous == null) { first = first.next; } else { previous.next = current.next; } } public Object find(int key) { steps = 0; Object current = first; while (current != null) { steps++; if (current.getKey() == key) { System.out.println("Schritte: " + steps); return current; } current = current.next; } System.out.println("Schritte: " + steps); return null; } public void displayList() { System.out.print("List: "); Object current = first; while (current != null) { current.displayLink(); current = current.next; } System.out.println(""); } return this; } public Object HashChain(int size) { private int arraySize = size; private Object[] hashArray = new Object[arraySize]; for (int i = 0; i < arraySize; i++) { hashArray[i] = List(); } public void displayTable() { for (int j = 0; j < arraySize; j++) { System.out.print(j + ". "); hashArray[j].displayList(); } } public int hashFunc(int key) { return key % arraySize; } public void insert(theLink) { int key = theLink.getKey(); int hashVal = hashFunc(key); hashArray[hashVal].insert(theLink); } public void delete(int key) { int hashVal = hashFunc(key); hashArray[hashVal].delete(key); } public Object find(int key) { int hashVal = hashFunc(key); Object theLink = hashArray[hashVal].find(key); return theLink; } return this; } public void fillHashTable(Object hashTable){ for (int i = 0; i < initSize; i++) { key = (int) (java.lang.Math.random() * 10 * size); dataItem = Link(key); hashTable.insert(dataItem); } }
size = 1; initSize = 14; hashTable = HashChain(size); fillHashTable(hashTable); hashTable.displayTable();
ausführen
size = 17; initSize = 14; hashTable = HashChain(size); fillHashTable(hashTable); key = (int) (java.lang.Math.random() * 10 * size); dataItem = hashTable.find(key); if (dataItem != null) { System.out.println(key + " wurde gefunden!"); } else { System.out.println(key + " konnte nicht gefunden werden!"); }
ausführen
Seite
Quelltext
Versionen/Autoren
Konto registrieren
Anmelden
Navigation
Startseite
Letzte Änderungen
Alle Seiten
Einführungskurs
Hilfe
Dateien
Suche
Suchen
Werkzeuge
Links auf diese Seite
Spezialseiten