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.
262 lines
6.9 KiB
262 lines
6.9 KiB
/**
|
|
* Sparse Implementierung einer verkettete Liste
|
|
*/
|
|
public class SparseVector {
|
|
|
|
/**
|
|
* [Beschreibung]
|
|
*
|
|
* @author Paul
|
|
*/
|
|
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;
|
|
}
|
|
|
|
}
|
|
|
|
// Elif: Standard Konstruktor mit einem leeren Vektor initialisieren
|
|
private Node head = null;
|
|
private int length = 0;
|
|
|
|
/**
|
|
* Konstruktor mit Vektor länge 0
|
|
*
|
|
* @author Elif
|
|
*/
|
|
public SparseVector() {
|
|
this.head = null;
|
|
this.length = 0;
|
|
}
|
|
|
|
/**
|
|
* Konstruktor mit Vektor länge n
|
|
*
|
|
* @author Elif
|
|
* @param n Vektor länge
|
|
*/
|
|
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
|
|
*
|
|
* @author Elif
|
|
* @param index [Beschreibung]
|
|
* @param value [Beschreibung]
|
|
*/
|
|
public void setElement(int index, double value) {
|
|
|
|
removeElement(index);
|
|
|
|
int maxIndex = this.getLength() - 1;
|
|
|
|
if (index > maxIndex) {
|
|
System.out.println("Fehler: Der Index " + index + " ist außerhalb des erlaubten Bereichs.");
|
|
return;
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* return the Wert value of that index input das Index, und return den
|
|
* entsprechenden Wert des Index
|
|
*
|
|
* @author Paul
|
|
* @param index [Beschreibung]
|
|
* @return double [Beschreibung]
|
|
*/
|
|
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) {
|
|
if (now.index == index) {
|
|
return now.value;
|
|
}
|
|
now = now.next;
|
|
}
|
|
|
|
return 0.0;
|
|
}
|
|
|
|
/**
|
|
* entfernt Element nach Index
|
|
*
|
|
* @author Leonhard
|
|
* @param index [Beschreibung]
|
|
*/
|
|
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
|
|
*
|
|
* @author Leonhard
|
|
* @return int [Beschreibung]
|
|
*/
|
|
public int getLength() {
|
|
|
|
int length = SparseVector.this.length;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
/**
|
|
* testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
|
|
*
|
|
* @author Terry
|
|
* @param other [Beschreibung]
|
|
* @return boolean [Beschreibung]
|
|
*/
|
|
public boolean equals(SparseVector other) {
|
|
|
|
if (this.getLength() != other.getLength()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* to add two vectors together and renew (overwrite) the this.vector
|
|
*
|
|
* @author Terry
|
|
* @param other [Beschreibung]
|
|
*/
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|