1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
|
public class SortierteListe<E extends Comparable<E>> {
private int size;
private Entry<E> head;
public SortierteListe() {
this.head = null;
}
public E get(int index) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
+ this.size);
}
Entry<E> currentEntry = this.head;
while (index > 0) {
currentEntry = currentEntry.getNext();
index--;
}
return currentEntry.getElement();
}
public int size() {
return this.size;
}
public int indexof(E element) {
int i = 0;
while (i < this.size) {
Entry<E> x = new Entry(element, this.head); // bei der Suche sollst
// du keine neuen
// Objekte erzeugen
if (x.getElement().equals(element)) {
return i;
} else {
this.head = x.getNext(); // damit löschst du die Liste, weil du
// den Kopf überschreibst und somit
// keine Referenz mehr auf ihn hast
i++;
}
}
return -1; // Wert zurückgeben, wenn nichts gefunden
}
public void add(E element) {
Entry<E> x = new Entry(element, this.head);
this.head = x;
this.size++;
// du sollst es sortiert einfügen, nicht an die Spitze setzen
}
private static class Entry<E> {
private E element;
private Entry<E> next;
public Entry(E o, Entry<E> next) {
this.element = o;
this.next = next;
}
public E getElement() {
return this.element;
}
public void setElement(E element) {
this.element = element;
}
public Entry<E> getNext() {
return this.next;
}
public void setNext(Entry<E> next) {
this.next = next;
}
}
} |