Versionsvergleich

Aus ProgrammingWiki

Vergleich: 2012-03-28T15:08:30+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;
−
		System.out.print("Einfügen von: ");
−
		theLink.displayLink();
+
	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("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 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) {
−
			first = first.next;
−
		} else {
−
			previous.next = current.next;
−
                }
+
                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) {
−
				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("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;
+
        for (int i = 0; i < numberOfKeys; i++) {
+
	        key = (int) (Math.random() * 10 * size);
+
	        elem = Elem(key);
+
	        hashTable.insert(elem);
+
        }
+
}
+
</code></popup>
+
<run id="4f6ca2b73358e">
+
size = 17;
+
n = 10;
−
        private Object[] hashArray = new Object[arraySize];
+
hashTable = HashTable(size);
+
fillHashTable(hashTable, n);
+
hashTable.displayTable();
+
</run>
−
        for (int i = 0; i < arraySize; i++) {
−
		hashArray[i] = List();
−
	}
+
<run id="4f74a205a5d00">
+
size = 17;
+
n = 10;
−
	public void displayTable() {
−
		for (int j = 0; j < arraySize; j++) {
−
			System.out.print(j + ". ");
−
			hashArray[j].displayList();
−
		}
−
	}
+
hashTable = HashTable(size);
+
fillHashTable(hashTable, n);
−
	public int hashFunc(int key) {
−
		return key % arraySize;
−
	}
+
hashTable.displayTable();
−
	public void insert(theLink) {
−
		int key = theLink.getKey();
−
		int hashVal = hashFunc(key);
−
		hashArray[hashVal].insert(theLink);
−
	}
+
key = (int) (Math.random() * 10 * size);
+
dataItem = hashTable.find(key);
−
	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;
+
if (dataItem != null) {
+
	System.out.println(key + " wurde gefunden!");
+
} else {
+
	System.out.println(key + " konnte nicht gefunden werden!");
}
+
</run>
−
public void fillHashTable(Object hashTable){
−
        keysPerCell = 10;
−
        for (int i = 0; i < initSize; i++) {
−
	        key = (int) (java.lang.Math.random() * keysPerCell * size);
−
	        dataItem = Link(key);
−
	        hashTable.insert(dataItem);
−
        }
−
}
−
</code>
−
</popup>
−
−
<run id="4f6a410984ee7">
+
<run id="4f74a7ce5891c">
size = 17;
−
initSize = 14;
+
n = 10;
−
hashTable = HashChain(size);
−
fillHashTable(hashTable);
+
hashTable = HashTable(size);
+
fillHashTable(hashTable, n);
+
hashTable.displayTable();
−
</run>
−
<run id="4f6a410984ee7">
−
size = 17;
−
initSize = 14;
+
key = (int) (Math.random() * 10 * size);
+
dataItem = hashTable.remove(key);
−
hashTable = HashChain(size);
−
fillHashTable(hashTable);
+
if (dataItem != null) {
+
	System.out.println(key + " wurde gelöscht!");
+
} else {
+
	System.out.println(key + " konnte nicht gelöscht werden!");
+
}
+
+
hashTable.displayTable();
</run>