verbesserung durch jessi

master
dfvcjal 2023-12-18 16:41:22 +01:00
parent 2e1c84a212
commit 7eb050d9d3
2 changed files with 73 additions and 33 deletions

View File

@ -5,59 +5,97 @@
* @author (Ihr Name) * @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum) * @version (eine Versionsnummer oder ein Datum)
*/ */
public class List<T> public class List<T>
{ {
private Node<T> first; private Node<T> first;
public List(){ //leere liste soll erzeugt werden, wert wird zugedingst
public List() {
first = null; first = null;
} }
public boolean isEmpty(){
if (first == null)return true; public boolean isEmpty() {
if (first == null) return true;
return false; return false;
} }
public int size(){
public int size() {
Node<T> current = first; Node<T> current = first;
int count = 0; int count = 0;
while (current != null){
while (current != null) {
count++; count++;
current = current.next; current = current.next;
} }
return count; return count;
} }
public T get(int n){
public T get(int n) {
Node<T> current = first; Node<T> current = first;
for(int i = 0; i<n; i++){
for (int i = 0; i < n; i++) {
if (current == null) return null; if (current == null) return null;
current = current.next; current = current.next;
} }
if (current == null) return null; if (current == null) return null;
return current.wert; return current.wert;
} }
public void add(T val){
public void add(T val) {
Node<T> neu = new Node<T>(); Node<T> neu = new Node<T>();
neu.wert = val; neu.wert = val;
if (first ==null){ if (first == null) {
first = neu; first = neu;
}else{ } else {
Node<T> current = first; Node<T> current = first;
while (current.next !=null){ while (current.next != null) {
current = current.next; current = current.next;
} }
current.next = neu; current.next = neu;
} }
} }
public void add(int n, T val){
Node<T> added public void add(int n, T val) {
} if (n>= size()){ //überprüfung am anfang wenn n >= ist wie die länge, also zahlen die zu lang sind
public boolean contains(T val){ add(val); //wenn n zu groß dann hänge am ende an...
Node<T> current = first; return; // und beende die Methode
while (current != null){
if (current.wert.equals(val)) return true;
} }
return false;
} Node<T> neu = new Node <T>();
public T remove(int n){ neu.wert = val;
if (n == 0){ // Wenn Nachfolger an stelle 0 einfügen möchte dann
neu.next = first;// Nachfolger auf bisher ersten(first) setzten
first = neu;// Neuer Eintrag ist der neue Erste(first)
return; //beenden
}
Node<T> current = first;
for (int i = 0; i < n-1; i++){
current = current.next;
}
neu.next =current.next;
current.next = neu;
} }
public boolean contains(T val) {
Node<T> current = first;
while (current != null) {
if (current.wert.equals(val)) return true;
}
return false;
}
public T remove(int n) {
return null; //Platzhalter
}
} }

View File

@ -5,17 +5,19 @@
* @author (Ihr Name) * @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum) * @version (eine Versionsnummer oder ein Datum)
*/ */
public class Node<T>
public class Node<T> //T für Typ und die Variable die wir hsben möchten (also int,string,...)
{ {
public T wert; public T wert;
public Node next; public Node<T> next;
public Node(T w){
this.wert = w;
} public void setWert(T w){ //statt int T damit es allgemeiner ist
public void setWert(T w){ this.wert = w;
this.wert = w; }
}
public void setNext(Node n){
this.next = n; public void setNext(Node n){
} this.next = n;
}
} }