You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
public class SparseVector {
private class Node { int index; double value; Node next;
public Node(int index, double value, Node next) { this.index = index; this.value = value; this.next = next; }
}
// Standard Konstruktor mit einem leeren Vektor initialisieren
private Node head = null; private int length = 0;
public SparseVector() { this.head = null; this.length = 0; }
// Konstruktor mit Vektor länge n
public SparseVector (int n) { this.head = null; this.length = n; }
/* den value in index hinzufügen - Aktualiiseren des Wertes, wenn der head an dem Index mit einem Wert exisitiert - Neuer head mit neuem Wert hinzufügen, wenn der head an dem Index nicht existiert */ public void setElement(int index, double value) {
removeElement(index);
if (this.head == null || this.head.index > index) { this.head = new Node(index, value, this.head);
} //current = now
Node now = this.head; while (now.next != null && now.next.index < index) { now = now.next; }
// gesuchte index gefunden
if (now != null && now.index == index) {
now.value = value; System.out.println("at index " + index + " set value " + value);
} else if (now.next == null || now.next.index > index) { now.next = new Node(index, value, now.next); System.out.println("at index " + index + " set value " + value);
}
}
/* getElement: return the Wert value of that index input das Index, und return den entsprechenden Wert des Index */
public double getElement(int index) {
if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
throw new IndexOutOfBoundsException("Index " + index + " ist außerhalb der Vektorlänge " + this.length + ". Max. Index ist " + (this.length-1)); }
Node now = this.head;
while (now != null && now.index < index) {
now = now.next; }
if (now != null && now.index == index) {
double result = now.value;
return result;
}
return 0.0;
}
// entfernt Element nach Index
public void removeElement(int index) { // previous = pre
Node now = this.head; Node pre = null;
if (now != null && now.index == index) { this.head = now.next; return;
}
while (now != null && now.index != index) {
pre = now; now = now.next;
} if (now != null && now.index == index) {
pre.next = now.next; // System.out.println("index " + index + " found and the value " + now.value + " deleted.");
} else {
// System.out.println("Element not found with index " + index);
} }
// Länge des Vektors ausgeben
public int getLength() {
int length = SparseVector.this.length;
return length;
}
// bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
public boolean equals(SparseVector other) {
Node thisnow = this.head; Node othernow = other.head;
while (thisnow != null || othernow != null) {
if (thisnow != null && othernow != null) { if (!(thisnow.index == othernow.index && thisnow.value == othernow.value)) { return false; }
thisnow = thisnow.next; othernow = othernow.next;
// wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen = nicht identisch
} else if ((thisnow != null && othernow == null) || (othernow != null && thisnow == null)) {
return false; }
} return true; }
// void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
Node thisnow = this.head; Node othernow = other.head; Node thispre = null;
// Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
if (this.getLength() != other.getLength()) {
System.out.println("Vektoren mit unterschiedlichen Längen können nicht zusammen addiert werden!!"); System.out.println("Länge des controlVector: " + this.getLength() + " aber die von otherVector: " + other.getLength()); return;
}
while (thisnow != null && othernow != null) {
// Fall 1, gleicher Index, dann nur Werte zusammen addieren → update this Vektor
if (thisnow.index == othernow.index ) { thisnow.value += othernow.value; // overwrite the this. value
thispre = thisnow; thisnow = thisnow.next; othernow = othernow.next; }
// Fall 2: Der Index von othernow ist kleiner, füge diesen Knoten in this ein
else if (othernow.index < thisnow.index) {
Node newNode = new Node(othernow.index, othernow.value, thisnow);
if (thispre == null) { this.head = newNode; } else { thispre.next = newNode; }
// this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
othernow = othernow.next; thispre = newNode;
// Fall 3: Der Index von othernow > thisnow, 2 Zeiger gehen weiter
} else if (othernow.index > thisnow.index) {
thispre = thisnow; thisnow = thisnow.next;
}
}
} }
|