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.

221 lines
6.5 KiB

7 months ago
7 months ago
7 months ago
7 months ago
  1. public class SparseVector {
  2. private class Node {
  3. int index;
  4. double value;
  5. Node next;
  6. public Node(int index, double value, Node next) {
  7. this.index = index;
  8. this.value = value;
  9. this.next = next;
  10. }
  11. }
  12. // Standard Konstruktor mit einem leeren Vektor initialisieren
  13. private Node head = null;
  14. private int length = 0;
  15. public SparseVector() {
  16. this.head = null;
  17. this.length = 0;
  18. }
  19. // Konstruktor mit Vektor länge n
  20. public SparseVector (int n) {
  21. this.head = null;
  22. this.length = n;
  23. }
  24. /* den value in index hinzufügen
  25. - Aktualiiseren des Wertes, wenn der head an dem Index mit einem Wert exisitiert
  26. - Neuer head mit neuem Wert hinzufügen, wenn der head an dem Index nicht existiert
  27. */
  28. public void setElement(int index, double value) {
  29. removeElement(index);
  30. if (this.head == null || this.head.index > index) {
  31. this.head = new Node(index, value, this.head);
  32. }
  33. //current = now
  34. Node now = this.head;
  35. while (now.next != null && now.next.index < index) {
  36. now = now.next;
  37. }
  38. // gesuchte index gefunden
  39. if (now != null && now.index == index) {
  40. now.value = value;
  41. System.out.println("at index " + index + " set value " + value);
  42. } else if (now.next == null || now.next.index > index) {
  43. now.next = new Node(index, value, now.next);
  44. System.out.println("at index " + index + " set value " + value);
  45. }
  46. }
  47. /* getElement: return the Wert value of that index
  48. input das Index, und return den entsprechenden Wert des Index
  49. */
  50. public double getElement(int index) {
  51. if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
  52. throw new IndexOutOfBoundsException("Index " + index + " ist außerhalb der Vektorlänge " + this.length + ". Max. Index ist " + (this.length-1));
  53. }
  54. Node now = this.head;
  55. while (now != null && now.index < index) {
  56. now = now.next;
  57. }
  58. if (now != null && now.index == index) {
  59. double result = now.value;
  60. return result;
  61. }
  62. return 0.0;
  63. }
  64. // entfernt Element nach Index
  65. public void removeElement(int index) {
  66. // previous = pre
  67. Node now = this.head;
  68. Node pre = null;
  69. if (now != null && now.index == index) {
  70. this.head = now.next;
  71. return;
  72. }
  73. while (now != null && now.index != index) {
  74. pre = now;
  75. now = now.next;
  76. }
  77. if (now != null && now.index == index) {
  78. pre.next = now.next;
  79. // System.out.println("index " + index + " found and the value " + now.value + " deleted.");
  80. } else {
  81. // System.out.println("Element not found with index " + index);
  82. }
  83. }
  84. // Länge des Vektors ausgeben
  85. public int getLength() {
  86. int length = SparseVector.this.length;
  87. return length;
  88. }
  89. // bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
  90. public boolean equals(SparseVector other) {
  91. Node thisnow = this.head;
  92. Node othernow = other.head;
  93. while (thisnow != null || othernow != null) {
  94. if (thisnow != null && othernow != null) {
  95. if (!(thisnow.index == othernow.index && thisnow.value == othernow.value)) {
  96. return false;
  97. }
  98. thisnow = thisnow.next;
  99. othernow = othernow.next;
  100. // wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen = nicht identisch
  101. } else if ((thisnow != null && othernow == null) || (othernow != null && thisnow == null)) {
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. // void add: to add two vectors together and renew (overwrite) the this.vector
  108. public void add(SparseVector other) {
  109. Node thisnow = this.head;
  110. Node othernow = other.head;
  111. Node thispre = null;
  112. // Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
  113. if (this.getLength() != other.getLength()) {
  114. System.out.println("Vektoren mit unterschiedlichen Längen können nicht zusammen addiert werden!!");
  115. System.out.println("Länge des controlVector: " + this.getLength() + " aber die von otherVector: " + other.getLength());
  116. return;
  117. }
  118. while (thisnow != null && othernow != null) {
  119. // Fall 1, gleicher Index, dann nur Werte zusammen addieren → update this Vektor
  120. if (thisnow.index == othernow.index ) {
  121. thisnow.value += othernow.value; // overwrite the this. value
  122. thispre = thisnow;
  123. thisnow = thisnow.next;
  124. othernow = othernow.next;
  125. }
  126. // Fall 2: Der Index von othernow ist kleiner, füge diesen Knoten in this ein
  127. else if (othernow.index < thisnow.index) {
  128. Node newNode = new Node(othernow.index, othernow.value, thisnow);
  129. if (thispre == null) {
  130. this.head = newNode;
  131. } else {
  132. thispre.next = newNode;
  133. }
  134. // this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
  135. othernow = othernow.next;
  136. thispre = newNode;
  137. // Fall 3: Der Index von othernow > thisnow, 2 Zeiger gehen weiter
  138. } else if (othernow.index > thisnow.index) {
  139. thispre = thisnow;
  140. thisnow = thisnow.next;
  141. }
  142. }
  143. }
  144. }