Browse Source

Leo: Leeren Ordner entfernt und Terrys Full Version Ordner hinzugefügt

master
fdai7303 6 months ago
parent
commit
e4356ca788
  1. 0
      AlgoDat/CheckSparseVector.java
  2. 130
      FullVersion(fromElsewhere)/CheckSparseVector.java
  3. 227
      FullVersion(fromElsewhere)/SparseVector.java

0
AlgoDat/CheckSparseVector.java

130
FullVersion(fromElsewhere)/CheckSparseVector.java

@ -0,0 +1,130 @@
public class CheckSparseVector {
public static void main(String[] args) {
//(ausgelagert)
// erstellen des this.Vektors = control Vector
SparseVector controlVector = new SparseVector(10);
controlVector.setElement(0, 10.0);
controlVector.setElement(5, 123.213);
controlVector.setElement(8, 65.01);
controlVector.setElement(9, 112.79);
System.out.println("\n");
// erstellen der testVektoren (the 5 "others")
SparseVector otherVector1 = new SparseVector(10);
otherVector1.setElement(0, 10.0);
otherVector1.setElement(5, 123.213);
otherVector1.setElement(8, 65.01);
otherVector1.setElement(9, 112.79);
System.out.println("\n");
SparseVector otherVector2 = new SparseVector(5);
otherVector2.setElement(0, 11.0);
otherVector2.setElement(3, 22.2);
System.out.println("\n");
SparseVector otherVector3 = new SparseVector(20);
otherVector3.setElement(0,123);
otherVector3.setElement(5,11);
otherVector3.setElement(11,2);
otherVector3.setElement(14,3);
otherVector3.setElement(18,4);
System.out.println("\n");
SparseVector otherVector4 = new SparseVector(10); // ein leerer other. Vektor mit Länge 10
System.out.println("der Wert des otherVector4 an der Position 5 ist: "+ otherVector4.getElement(5)); // 0.0
System.out.println("der Wert des otherVector4 an der Position 7 ist: "+ otherVector4.getElement(7));// 0.0
System.out.println("der Wert des otherVector4 an der Position 9 ist: " + otherVector4.getElement(9));// 0.0
System.out.println("die Länge des otherVector4 ist: " + otherVector4.getLength()); // 10
System.out.println("\n");
SparseVector otherVector5 = new SparseVector(20); // ein leerer other. Vektor mit Länge 10
System.out.println("der Wert des otherVector5 an der Position 5 ist: "+ otherVector5.getElement(5)); // 0.0
System.out.println("der Wert des otherVector5 an der Position 17 ist: "+ otherVector5.getElement(17));// 0.0
System.out.println("der Wert des otherVector5 an der Position 19 ist: " + otherVector5.getElement(19));// 0.0
System.out.println("die Länge des otherVector5 ist: " + otherVector5.getLength()); // 20
System.out.println("\n");
// Testen des this. Vektors --> testen Methoden wie getLength(), getElement(), setElement(), removeElement()
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 123.213
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); //10.0
System.out.println("der Wert des controlVector an der Position 1 ist: " + controlVector.getElement(1)); //0.0
// testen equal(), wenn die beiden Vektoren identisch sind should be true
System.out.println(controlVector.equals(otherVector1));
System.out.println("\n");
controlVector.removeElement(5);
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 0.0
System.out.println("Die Länge nach remove ist: "+ controlVector.getLength()); // 10
controlVector.removeElement(7);
System.out.println("der Wert des controlVector an der Position 7 ist: "+ controlVector.getElement(7)); // 0.0
controlVector.setElement(5, 100);
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); //100.0
System.out.println("\n");
// testen equals(other) --> nicht identisch
System.out.println(controlVector.equals(otherVector1)); // should be false, weil der Wert an Index 5 zu 100 gesetzt wurde
System.out.println(controlVector.equals(otherVector2)); // should be false
System.out.println(controlVector.equals(otherVector3)); // should be false
System.out.println(controlVector.equals(otherVector4)); // should be false
System.out.println("\n");
otherVector1.setElement(5, 100);
System.out.println("der Wert des otherVector1 an der Position 5 ist: "+ otherVector1.getElement(5)); //100.0
System.out.println(otherVector1.equals(controlVector));// should be true, weil der Wert an Index 5 von this und other Vektor gleich 100
System.out.println("\n");
// testen add()
controlVector.add(otherVector1);
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 10 + 10 = 20
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 100 + 100 = 200
System.out.println("der Wert des controlVector an der Position 6 ist: " + controlVector.getElement(6)); // 0.0
System.out.println("der Wert des controlVector an der Position 8 ist: " + controlVector.getElement(8)); // 65.01 + 65.01 = 130.02
System.out.println("der Wert des controlVector an der Position 9 ist: " + controlVector.getElement(9)); // 112.79 + 112.79 = 225.58
System.out.println("\n");
controlVector.add(otherVector2);
controlVector.setElement(3,90.1);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 3 ist: " + controlVector.getElement(3)); // 90.1
System.out.println("\n");
controlVector.add(otherVector3);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("der Wert des controlVector an der Position 9 ist: "+ controlVector.getElement(9)); // 225.58
System.out.println("\n");
controlVector.add(otherVector4);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0));
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5));
System.out.println("\n");
controlVector.removeElement(0);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("\n");
controlVector.add(otherVector5);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("\n");
}
}

227
FullVersion(fromElsewhere)/SparseVector.java

@ -0,0 +1,227 @@
public class SparseVector {
// Hilfsklasse Node
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: leeren Vektor initialisieren
private Node head = null;
private int length = 0;
public SparseVector() {
this.head = null;
this.length = 0;
}
// Konstruktor: erzeugen Vektor mit Lange n
public SparseVector (int n) {
this.head = null;
this.length = n;
}
/* setElement(int index, double value): add value into each index
(case1: wenn der Knoten an dem Index mit einem Wert existiert, den Wert aktualisieren)
(case2: wenn der Knoten an dem Index noch nicht existiert, ein neuer Knoten mit neuem Wert hinzüfugen)
*/
public void setElement(int index, double value) { //z.B. wenn ich zu index (6) einen value (1) einfüge,
removeElement(index);
if (this.head == null || this.head.index > index) {
// aber entweder Element nicht existiert oder das erste Element ist > 6 (Start: index 7)
this.head = new Node(index, value, this.head); // erzeugen eines neuen Kopfs
}
Node current = this.head;
while (current.next != null && current.next.index < index) { // Zeiger bewegt sich durch den Vektor, solange der gesuchte Index noch nicht erreicht wird
current = current.next;
}
if (current != null && current.index == index) { // und der gesuchte Index erreicht wird
current.value = value;
System.out.println("at index " + index + " set value " + value);
} else if (current.next == null || current.next.index > index) { // zum Beispiel, wenn Zeiger am Ende ankommt, ohne den gesuchten Index
// oder next index ist 7 und ich brauche index 6, einfügen das Element index 6 vor index 7
current.next = new Node(index, value, current.next); // siehe ipad Testat1 setElement
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 current = this.head;
while (current != null && current.index < index) {
current = current.next;
}
if (current != null && current.index == index) {
double result = current.value;
return result;
}
return 0.0;
}
// removeElement: remove the whole element per index
public void removeElement(int index) { // siehe ipad Testat1 removeElement
Node current = this.head;
Node previous = null;
if (current != null && current.index == index) {
this.head = current.next;
System.out.println(index + " found and deleted");
return;
}
while (current != null && current.index != index) {
previous = current;
current = current.next;
}
if (current != null && current.index == index) {
previous.next = current.next;
System.out.println("index " + index + " found and the value " + current.value + " deleted.");
} else {
System.out.println("Element not found with index " + index);
}
}
// getLength: return the length (max. capacity) of Vektor
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 thisCurrent = this.head;
Node otherCurrent = other.head;
while (thisCurrent != null || otherCurrent != null) {
if (thisCurrent != null && otherCurrent != null) {
if (!(thisCurrent.index == otherCurrent.index && thisCurrent.value == otherCurrent.value)) {
return false;
}
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
// wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen = nicht identisch
} else if ((thisCurrent != null && otherCurrent == null) || (otherCurrent != null && thisCurrent == null)) {
return false;
}
}
return true;
}
// void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
Node thisCurrent = this.head;
Node otherCurrent = other.head;
Node thisPrevious = 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 (thisCurrent != null && otherCurrent != null) {
// Fall 1, gleicher Index, dann nur Werte zusammen addieren update this Vektor
if (thisCurrent.index == otherCurrent.index ) {
thisCurrent.value += otherCurrent.value; // overwrite the this. value
thisPrevious = thisCurrent;
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
}
// Fall 2: Der Index von otherCurrent ist kleiner, füge diesen Knoten in this ein
else if (otherCurrent.index < thisCurrent.index) {
Node newNode = new Node(otherCurrent.index, otherCurrent.value, thisCurrent);
if (thisPrevious == null) {
this.head = newNode;
} else {
thisPrevious.next = newNode;
}
// this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
otherCurrent = otherCurrent.next;
thisPrevious = newNode;
// Fall 3: Der Index von otherCurrent > thisCurrent, 2 Zeiger gehen weiter
} else if (otherCurrent.index > thisCurrent.index) {
thisPrevious = thisCurrent;
thisCurrent = thisCurrent.next;
}
}
}
}
Loading…
Cancel
Save