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.

266 lines
8.0 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. /**
  2. * Sparse Implementierung einer verkettete Liste
  3. */
  4. public class SparseVector {
  5. /**
  6. * Hilfsklasse, welche ein einzelnes Node repräsentiert.
  7. *
  8. * @author Paul
  9. */
  10. private class Node {
  11. int index;
  12. double value;
  13. Node next;
  14. public Node(int index, double value, Node next) {
  15. this.index = index;
  16. this.value = value;
  17. this.next = next;
  18. }
  19. }
  20. // Standard Konstruktor mit einem leeren Vektor initialisieren
  21. private Node firstNode = null;
  22. private int length = 0;
  23. /**
  24. * Konstruktor mit Vektor länge 0
  25. *
  26. * @author Elif
  27. */
  28. public SparseVector() {
  29. this.firstNode = null;
  30. this.length = 0;
  31. }
  32. /**
  33. * Konstruktor mit Vektor länge n
  34. *
  35. * @author Elif
  36. * @param n Vektor länge
  37. */
  38. public SparseVector(int n) {
  39. this.firstNode = null;
  40. this.length = n;
  41. }
  42. /**
  43. * den value in index hinzufügen
  44. * - Aktualisieren des Wertes, wenn der firstNode an dem Index mit einem Wert
  45. * exisitiert
  46. * - Neuer firstNode mit neuem Wert hinzufügen, wenn der firstNode an dem Index
  47. * nicht existiert
  48. *
  49. * @author Elif
  50. * @param index Stelle,w wo Vektor eingefügt werden soll
  51. * @param value wert, den wir übergeben
  52. */
  53. public void setElement(int index, double value) {
  54. // methode wird aufgerufen, falls index existiert = platz schaffen
  55. removeElement(index);
  56. int maxIndex = this.getLength() - 1;
  57. // 1. if: schauen, ob Index im erlaubten Bereich
  58. // 2. if: schauen, ob erster Knoten null ist und index von erstem knoten größer
  59. // als index, dann neuen knoten setzen
  60. if (index > maxIndex) {
  61. System.out.println("Fehler: Der Index " + index + " ist außerhalb des erlaubten Bereichs.");
  62. return;
  63. }
  64. if (this.firstNode == null || this.firstNode.index > index) {
  65. this.firstNode = new Node(index, value, this.firstNode);
  66. }
  67. // hilfsknoten erstellen
  68. // solange im index und gesuchten index nicht erreicht haben, gehen wir weiter
  69. Node now = this.firstNode;
  70. while (now.next != null && now.next.index < index) {
  71. now = now.next;
  72. }
  73. // gesuchte index gefunden
  74. if (now != null && now.index == index) {
  75. now.value = value;
  76. System.out.println("at index " + index + " set value " + value);
  77. // knoten den wir erstellen wollten existiert nicht, deshlab new node
  78. } else if (now.next == null || now.next.index > index) {
  79. now.next = new Node(index, value, now.next);
  80. System.out.println("at index " + index + " set value " + value);
  81. }
  82. }
  83. /**
  84. * return the Wert value of that index input das Index, und return den
  85. * entsprechenden Wert des Index
  86. *
  87. * @author Paul
  88. * @param index Stelle, an der der Wert steht
  89. * @return double Wert des Elements
  90. */
  91. public double getElement(int index) {
  92. if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
  93. throw new IndexOutOfBoundsException("Index " + index + " ist außerhalb der Vektorlänge " + this.length
  94. + ". Max. Index ist " + (this.length - 1));
  95. }
  96. Node now = this.firstNode;
  97. while (now != null) {
  98. if (now.index == index) {
  99. return now.value;
  100. }
  101. now = now.next;
  102. }
  103. return 0.0;
  104. }
  105. /**
  106. * entfernt Element nach Index
  107. *
  108. * @author Leonhard
  109. * @param index Stelle des zu entferneden Elements
  110. */
  111. public void removeElement(int index) {
  112. // Anlegen der jetzigen und vorherigen Node
  113. Node now = this.firstNode;
  114. Node pre = null;
  115. // Wenn der momentane Node nicht Null ist und der gesuchte Index korrekt ist
  116. // ersetze den momentanen Node durch den nächsten
  117. if (now != null && now.index == index) {
  118. this.firstNode = now.next;
  119. return;
  120. }
  121. // Während wir momentan nicht bei Null sind und den Index nicht gefunden haben
  122. // setzte pre und now den nächsten Node weiter
  123. while (now != null && now.index != index) {
  124. pre = now;
  125. now = now.next;
  126. }
  127. // Wenn wir den Index gefunden haben und nicht bei Null sind
  128. // setze den nächsten Node auf den momentanen Node
  129. if (now != null && now.index == index) {
  130. pre.next = now.next;
  131. System.out.println("Node mit Wert: " + now.value + " am Index " + index + " gefunden und gelöscht.");
  132. } else {
  133. System.out.println("Keine Node am Index: " + index + " nicht gefunden!");
  134. }
  135. }
  136. /**
  137. * Gibt die Länge des Vektors aus
  138. *
  139. * @author Leonhard
  140. * @return int Vektor Länge
  141. */
  142. public int getLength() {
  143. return SparseVector.this.length;
  144. }
  145. /**
  146. * testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
  147. *
  148. * @author YC Terry
  149. * @param other Anderer Vektor, mit dem verglichen wird
  150. * @return boolean Wahrheitswert, ob Vektoren gleich sind
  151. */
  152. public boolean equals(SparseVector other) {
  153. if (this.getLength() != other.getLength()) {
  154. return false;
  155. }
  156. Node thisnow = this.firstNode;
  157. Node othernow = other.firstNode;
  158. while (thisnow != null || othernow != null) {
  159. if (thisnow != null && othernow != null) {
  160. if (!(thisnow.index == othernow.index && thisnow.value == othernow.value)) {
  161. return false;
  162. }
  163. thisnow = thisnow.next;
  164. othernow = othernow.next;
  165. // wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen
  166. // = nicht identisch
  167. } else if ((thisnow != null && othernow == null) || (othernow != null && thisnow == null)) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. * to add two vectors together and renew (overwrite) the this.vector
  175. *
  176. * @author YC Terry
  177. * @param other Anderer Vektor, der auf addiert wird
  178. */
  179. public void add(SparseVector other) {
  180. Node thisnow = this.firstNode;
  181. Node othernow = other.firstNode;
  182. Node thispre = null;
  183. // Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
  184. if (this.getLength() != other.getLength()) {
  185. System.out.println("Vektoren mit unterschiedlichen Längen können nicht zusammen addiert werden!!");
  186. System.out.println(
  187. "Länge des controlVector: " + this.getLength() + " aber die von otherVector: " + other.getLength());
  188. return;
  189. }
  190. while (thisnow != null && othernow != null) {
  191. // Fall 1, gleicher Index, dann nur Werte zusammen addieren → update this Vektor
  192. if (thisnow.index == othernow.index) {
  193. thisnow.value += othernow.value; // overwrite the this. value
  194. thispre = thisnow;
  195. thisnow = thisnow.next;
  196. othernow = othernow.next;
  197. }
  198. // Fall 2: Der Index von othernow ist kleiner, füge diesen Knoten in this ein
  199. else if (othernow.index < thisnow.index) {
  200. Node newNode = new Node(othernow.index, othernow.value, thisnow);
  201. if (thispre == null) {
  202. this.firstNode = newNode;
  203. } else {
  204. thispre.next = newNode;
  205. }
  206. // this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
  207. othernow = othernow.next;
  208. thispre = newNode;
  209. // Fall 3: Der Index von othernow > thisnow, 2 Zeiger gehen weiter
  210. } else if (othernow.index > thisnow.index) {
  211. thispre = thisnow;
  212. thisnow = thisnow.next;
  213. }
  214. }
  215. }
  216. }