Benutzer:Riemer

Aus ProgrammingWiki

Wiki-Quelltext dieser Seite.

<language>Java</language>
<popup name="Klassen-Definitionen">
<code id="4f6a410984ee7">
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;

		while (current != null && key != current.getKey()) {
			previous = current;
			current = current.next;
		}

		if (previous == null) {
			first = first.next;
		} else {
			previous.next = current.next;
                }
	}

	public Object find(int key) {
		Object current = first;
		while (current != null) {						
			if (current.getKey() == key) {
				return current;
                        }
			current = current.next;
		}
		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){
        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">
size = 17;
initSize = 14;

hashTable = HashChain(size);
fillHashTable(hashTable);
		
key = 100;
dataItem = Link(key);
hashTable.insert(dataItem);
		
hashTable.displayTable();

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">
hashTable.displayTable();
</run>