2 Commits
b6f9c471bf
...
3d239a8601
Author | SHA1 | Message | Date |
---|---|---|---|
fdai7303 | 3d239a8601 |
Merge branch 'master' of gogs.informatik.hs-fulda.de:fdai7303/AlgoDat
|
1 year ago |
fdai7303 | 310faffd86 |
Leo: Formatierung von SparseVector.java verschönert
|
1 year ago |
2 changed files with 100 additions and 348 deletions
@ -1,227 +0,0 @@ |
|||
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; |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
} |
|||
} |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue