Versionsvergleich

Aus ProgrammingWiki

Vergleich: 2012-03-28T16:54: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;
−
−
		if (first == null) {
−
                        steps++;
−
			first = theLink;
−
			last = first;
−
		} else {
−
                        steps++;
−
			last.next = theLink;
−
			last = last.next;
+
	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("Einfügen von: " + theLink.getKey() + " - Schritte: " + steps);
+
		System.out.println("");
	}
−
	public Object delete(int key) {
−
		Object previous = null;
−
		Object current = first;
−
                Object temp = null;
−
                steps = 1;
+
	int hashFunction(int key) {
+
		return key % arraySize;
+
	}
−
/*		while (current != null && key != current.getKey()) {
+
	void insert(elem) {
+
		steps = 0;
+
		int key = elem.getKey();
+
		int hashVal = hashFunction(key);
+
                int temp = hashVal;
+
+
		while (hashArray[hashVal] != null) {
                        steps++;
−
			previous = current;
−
			current = current.next;
+
			hashVal = temp + steps*steps;
+
			hashVal %= arraySize;
		}
+
		hashArray[hashVal] = elem;
+
		System.out.println(steps + " Sondierungen für " + key);
+
	}
−
		if (previous == null) {
−
                        temp = first;
−
			first = first.next;
−
                        System.out.println("Schritte: " + steps);
−
                        return temp;
−
		} else {
−
                        temp = current;
−
			previous.next = current.next;
−
                        System.out.println("Schritte: " + steps);
−
                        return current;
−
                }
−
       //         System.out.println("Schritte: " + steps);
−
       //         return null;*/
+
	Object remove(int key) {
+
                steps = 0;
+
                tempElem = null;
+
		int hashVal = hashFunction(key);
+
                int temp = hashVal;
−
while (current != null && key != current.getKey()) {
−
			steps++;
−
			previous = current;
−
			current = current.next;
−
−
			if (previous == null) {
−
				temp = first;
−
				first = first.next;
−
				System.out.println("Schritte: " + steps);
−
				return temp;
−
			} else {
−
				temp = current;
−
				previous.next = current.next;
−
				System.out.println("Schritte: " + steps);
−
				return current;
+
		while (hashArray[hashVal] != null) {
+
			if (hashArray[hashVal].getKey() == key) {
+
                                System.out.println("Sondierungen: " + steps);
+
                                tempElem = hashArray[hashVal];
+
				hashArray[hashVal] = null;
+
                                return tempElem;
			}
+
                        steps++;
+
			hashVal = temp + steps*steps;
+
			hashVal %= arraySize;
		}
−
		System.out.println("Schritte: " + steps);
+
                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="4f6a410984ee7">
−
size = 1;
−
numberOfKeys = 10;
+
<run id="4f74a7ce5891c">
+
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.delete(key);
+
dataItem = hashTable.remove(key);
if (dataItem != null) {
	System.out.println(key + " wurde gelöscht!");
} else {
−
	System.out.println(key + " konnte nicht gefunden werden!");
+
	System.out.println(key + " konnte nicht gelöscht werden!");
}
+
+
hashTable.displayTable();
</run>