Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2012-03-28T15:42:35+00:00 → 2012-03-30T11:17:14+00:00
<language>Java</language> | |
<popup name="Klassen-Definitionen"> | |
| − | <code id="4f6a410984ee7"> |
| − | public Object Link(int d) { |
| + | <code id="4f6ca2b733235"> |
| + | public Object Elem(int d) { |
int data = d; | |
| − | Object next; |
| − | |
| − | public int getKey() { |
| + | int getKey() { |
return data; | |
} | |
| − | |
| − | public void displayLink() { |
| − | System.out.print(data + " "); |
| − | } |
return this; | |
} | |
| + | public Object HashTable(int size) { |
| + | int arraySize = size; |
| + | int steps; |
| − | public Object List() { |
| − | Object first = null; |
| − | Object last = null; |
| − | int steps = 0; |
| + | Object[] hashArray = new Object[arraySize]; |
| + | for(i = 0; i < arraySize; i++) { |
| + | hashArray[i] = null; |
| + | } |
| − | public void insert(Object theLink) { |
| − | steps = 0; |
| + | void displayTable() { |
| + | for (int i = 0; i < arraySize; i++) { |
| + | if (hashArray[i] != null) |
| + | System.out.print(hashArray[i].getKey() + " "); |
| + | else |
| + | System.out.print("# "); |
| + | } |
| + | System.out.println(""); |
| + | } |
| − | if (first == null) { |
| + | int hashFunction(int key) { |
| + | return key % arraySize; |
| + | } |
| + | |
| + | void insert(elem) { |
| + | steps = 0; |
| + | int key = elem.getKey(); |
| + | int hashVal = hashFunction(key); |
| + | int temp = hashVal; |
| + | |
| + | while (hashArray[hashVal] != null) { |
steps++; | |
| − | first = theLink; |
| − | last = first; |
| − | } else { |
| − | steps++; |
| − | last.next = theLink; |
| − | last = last.next; |
| + | hashVal = temp + steps*steps; |
| + | hashVal %= arraySize; |
} | |
| − | System.out.println("Einfügen von: " + theLink.getKey() + " - Schritte: " + steps); |
| + | hashArray[hashVal] = elem; |
| + | System.out.println(steps + " Sondierungen für " + key); |
} | |
| − | public void delete(int key) { |
| − | Object previous = null; |
| − | Object current = first; |
| − | Object temp = null; |
| + | Object remove(int key) { |
steps = 0; | |
| + | tempElem = null; |
| + | int hashVal = hashFunction(key); |
| + | int temp = hashVal; |
| − | while (current != null && key != current.getKey()) { |
| + | while (hashArray[hashVal] != null) { |
| + | if (hashArray[hashVal].getKey() == key) { |
| + | System.out.println("Sondierungen: " + steps); |
| + | tempElem = hashArray[hashVal]; |
| + | hashArray[hashVal] = null; |
| + | return tempElem; |
| + | } |
steps++; | |
| − | previous = current; |
| − | current = current.next; |
| + | hashVal = temp + steps*steps; |
| + | hashVal %= arraySize; |
} | |
| − | |
| − | if (previous == null) { |
| − | temp = first; |
| − | first = first.next; |
| − | return temp; |
| − | } else { |
| − | temp = current; |
| − | previous.next = current.next; |
| − | return current; |
| − | } |
| − | return null; |
| + | System.out.println("Sondierungen: " + steps); |
| + | return null; |
} | |
| − | public Object find(int key) { |
| + | Object find(int key) { |
steps = 0; | |
| − | Object current = first; |
| − | while (current != null) { |
| − | steps++; |
| − | if (current.getKey() == key) { |
| − | System.out.println("Schritte: " + steps); |
| − | return current; |
| + | int hashVal = hashFunction(key); |
| + | int temp = hashVal; |
| + | |
| + | while (hashArray[hashVal] != null) { |
| + | if (hashArray[hashVal].getKey() == key) { |
| + | System.out.println("Sondierungen: " + steps); |
| + | return hashArray[hashVal]; |
} | |
| − | current = current.next; |
| + | steps++; |
| + | hashVal = temp + steps*steps; |
| + | hashVal %= arraySize; |
} | |
| − | System.out.println("Schritte: " + steps); |
| + | System.out.println("Sondierungen: " + 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) { |
| + | void fillHashTable(Object hashTable, int numberOfKeys) { |
| − | 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 Object delete(int key) { |
| − | int hashVal = hashFunc(key); |
| − | Object theLink = hashArray[hashVal].delete(key); |
| − | return theLink |
| − | } |
| − | |
| − | public Object find(int key) { |
| − | int hashVal = hashFunc(key); |
| − | Object theLink = hashArray[hashVal].find(key); |
| − | return theLink; |
| − | } |
| − | return this; |
| − | } |
| − | |
| − | public void fillHashTable(Object hashTable, int numberOfKeys){ |
for (int i = 0; i < numberOfKeys; i++) { | |
key = (int) (Math.random() * 10 * size); | |
| − | dataItem = Link(key); |
| − | hashTable.insert(dataItem); |
| + | elem = Elem(key); |
| + | hashTable.insert(elem); |
} | |
} | |
| − | </code> |
| − | </popup> |
| + | </code></popup> |
| + | <run id="4f6ca2b73358e"> |
| + | size = 17; |
| + | n = 10; |
| − | <run id="4f6a410984ee7"> |
| − | size = 1; |
| − | numberOfKeys = 10; |
| − | |
| − | hashTable = HashChain(size); |
| − | fillHashTable(hashTable, numberOfKeys); |
| + | hashTable = HashTable(size); |
| + | fillHashTable(hashTable, n); |
hashTable.displayTable(); | |
</run> | |
| − | <run id="4f6a410984ee7"> |
| − | size = 1; |
| − | numberOfKeys = 10; |
| + | <run id="4f74a205a5d00"> |
| + | size = 17; |
| + | n = 10; |
| − | hashTable = HashChain(size); |
| − | fillHashTable(hashTable, numberOfKeys); |
| + | hashTable = HashTable(size); |
| + | fillHashTable(hashTable, n); |
hashTable.displayTable(); | |
key = (int) (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!"); | |
} | |
</run> | |
| + | |
| + | <run id="4f74a7ce5891c"> |
| + | size = 17; |
| + | n = 10; |
| + | |
| + | hashTable = HashTable(size); |
| + | fillHashTable(hashTable, n); |
| + | |
| + | hashTable.displayTable(); |
| + | |
| + | key = (int) (Math.random() * 10 * size); |
| + | dataItem = hashTable.remove(key); |
| + | |
| + | if (dataItem != null) { |
| + | System.out.println(key + " wurde gelöscht!"); |
| + | } else { |
| + | System.out.println(key + " konnte nicht gelöscht werden!"); |
| + | } |
| + | |
| + | hashTable.displayTable(); |
| + | </run> |