2 Commits

Author SHA1 Message Date
elif.efe 660ff362fd Kommentare1 6 months ago
elif.efe 70fc2385e9 kommentare 6 months ago
  1. 140
      SparseVector.java

140
SparseVector.java

@ -21,8 +21,8 @@ public class SparseVector {
}
// Elif: Standard Konstruktor mit einem leeren Vektor initialisieren
private Node head = null;
// Standard Konstruktor mit einem leeren Vektor initialisieren
private Node firstNode = null;
private int length = 0;
/**
@ -31,7 +31,7 @@ public class SparseVector {
* @author Elif
*/
public SparseVector() {
this.head = null;
this.firstNode = null;
this.length = 0;
}
@ -42,36 +42,33 @@ public class SparseVector {
* @param n Vektor länge
*/
public SparseVector(int n) {
this.head = null;
this.firstNode = 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 Index des zu setzenden Wertes
* @param value Wert, welcher eingesetzt werden soll
*/
public void setElement(int index, double value) {
/* den value in index hinzufügen
- Aktualiiseren des Wertes, wenn der firstNode an dem Index mit einem Wert exisitiert
- Neuer firstNode mit neuem Wert hinzufügen, wenn der firstNode an dem Index nicht existiert
*/
public void setElement(int index, double value) {
// methode wird aufgerufen, falls index existiert = platz schaffen
removeElement(index);
int maxIndex = this.getLength() - 1;
// 1. if: schauen, ob Index im erlaubten Bereich
//2. if: schauen, ob erster Knoten null ist und index von erstem knoten größer als index, dann neuen knoten setzen
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);
if (this.firstNode == null || this.firstNode.index > index) {
this.firstNode = new Node(index, value, this.firstNode);
}
// current = now
Node now = this.head;
//hilfsknoten erstellen
//solange im index und gesuchten index nicht erreicht haben, gehen wir weiter
Node now = this.firstNode;
while (now.next != null && now.next.index < index) {
now = now.next;
@ -81,7 +78,7 @@ public class SparseVector {
if (now != null && now.index == index) {
now.value = value;
System.out.println("at index " + index + " set value " + value);
// knoten den wir erstellen wollten existiert nicht, deshlab new node
} 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);
@ -90,14 +87,11 @@ public class SparseVector {
}
/**
* return the Wert value of that index input das Index, und return den
* entsprechenden Wert des Index
*
* @author Paul
* @param index Stelle, an der der Wert steht
* @return double Wert des Elements
*/
/* 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
@ -105,7 +99,7 @@ public class SparseVector {
+ ". Max. Index ist " + (this.length - 1));
}
Node now = this.head;
Node now = this.firstNode;
while (now != null) {
if (now.index == index) {
@ -125,51 +119,49 @@ public class SparseVector {
*/
public void removeElement(int index) {
// Anlegen der jetzigen und vorherigen Node
Node now = this.head;
Node now = this.firstNode;
Node pre = null;
// Wenn der momentane Node nicht Null ist und der gesuchte Index korrekt ist
// ersetze den momentanen Node durch den nächsten
if (now != null && now.index == index) {
this.head = now.next;
this.firstNode = now.next;
return;
}
// Während wir momentan nicht bei Null sind und den Index nicht gefunden haben
// setzte pre und now den nächsten Node weiter
while (now != null && now.index != index) {
pre = now;
now = now.next;
}
while (now != null && now.index != index) {
// Wenn wir den Index gefunden haben und nicht bei Null sind
// setze den nächsten Node auf den momentanen Node
if (now != null && now.index == index) {
pre.next = now.next;
System.out.println("Node mit Wert: " + now.value + " am Index " + index + " gefunden und gelöscht.");
} else {
System.out.println("Keine Node am Index: " + index + " nicht gefunden!");
}
}
pre = now;
now = now.next;
/**
* Länge des Vektors ausgeben
*
* @author Leonhard
* @return int [Beschreibung]
*/
public int getLength() {
return SparseVector.this.length;
}
}
if (now != null && now.index == index) {
/**
* testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
*
* @author YC Terry
* @param other Anderer Vektor, mit dem verglichen wird
* @return boolean Wahrheitswert, ob Vektoren gleich sind
*/
public boolean equals(SparseVector other) {
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
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) {
if (this.getLength() != other.getLength()) {
@ -177,8 +169,8 @@ public class SparseVector {
}
Node thisnow = this.head;
Node othernow = other.head;
Node thisnow = this.firstNode;
Node othernow = other.firstNode;
while (thisnow != null || othernow != null) {
@ -202,16 +194,12 @@ public class SparseVector {
return true;
}
/**
* to add two vectors together and renew (overwrite) the this.vector
*
* @author YC Terry
* @param other Anderer Vektor, der auf addiert wird
*/
public void add(SparseVector other) {
// void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
Node thisnow = this.head;
Node othernow = other.head;
Node thisnow = this.firstNode;
Node othernow = other.firstNode;
Node thispre = null;
// Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
@ -241,7 +229,7 @@ public class SparseVector {
Node newNode = new Node(othernow.index, othernow.value, thisnow);
if (thispre == null) {
this.head = newNode;
this.firstNode = newNode;
} else {
thispre.next = newNode;
}

Loading…
Cancel
Save