diff --git a/LinkedList.java b/LinkedList.java index 5aeef56..e115904 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -8,9 +8,7 @@ public class LinkedList // T muss immer in der Klasse gennant werden { private Node start; //Node braucht auvh Datentyp T - public LinkedList(){ - - } + public void einfuegen(T zahl){ // Lege einen neuen Knoten an mit der Zahl diff --git a/List.java b/List.java new file mode 100644 index 0000000..f1270d8 --- /dev/null +++ b/List.java @@ -0,0 +1,99 @@ + +/** + * Beschreiben Sie hier die Klasse List_T_. + * + * @author (Ihr Name) + * @version (eine Versionsnummer oder ein Datum) + */ +public class List +{ + private Node first; + + public List() { + first = null; + } + + public boolean isEmpty() { + if (first == null) return true; + return false; + } + + public int size() { + Node current = first; + int count = 0; + + while (current != null) { + count++; + current = current.next; + } + + return count; + } + + public T get(int n) { + Node current = first; + + for (int i = 0; i < n; i++) { + if (current == null) return null; + current = current.next; + } + + if (current == null) return null; + return current.wert; + } + + public void add(T val) { + Node neu = new Node(); + neu.wert = val; + + if (first == null) { + first = neu; + } else { + Node current = first; + while (current.next != null) { + current = current.next; + } + current.next = neu; + } + } + + 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 + add(val); //wenn n zu groß dann hänge am ende an... + return; // und beende die Methode + } + + Node neu = new Node (); + 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 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 current = first; + + while (current != null) { + if (current.wert.equals(val)) return true; + } + + return false; + } + + public T remove(int n) { + return null; //Platzhalter + } +} diff --git a/Node.java b/Node.java index 57bd6d3..7714694 100644 --- a/Node.java +++ b/Node.java @@ -5,15 +5,17 @@ * @author (Ihr Name) * @version (eine Versionsnummer oder ein Datum) */ -public class Node //T für Typ und die Variable die wir hsben möchten (also int,string,...) +public class Node //T für Typ und die Variable die wir hsben möchten (also int,string,...) { public T wert; - public Node next; + public Node next; + public void setWert(T w){ //statt int T damit es allgemeiner ist this.wert = w; } + public void setNext(Node n){ this.next = n; } diff --git a/Node2.java b/Node2.java new file mode 100644 index 0000000..22356fb --- /dev/null +++ b/Node2.java @@ -0,0 +1,15 @@ + +/** + * Beschreiben Sie hier die Klasse NodeT2. + * + * @author (Ihr Name) + * @version (eine Versionsnummer oder ein Datum) + */ +public class Node2 +{ + public class Node +{ + public T wert; + public Node next; +} +} diff --git a/Test2.java b/Test2.java index 487835a..1499585 100644 --- a/Test2.java +++ b/Test2.java @@ -25,7 +25,7 @@ public class Test2 System.out.println(l.getNteZahl(23)); // gibt 0 aus weil Liste nicht so lang ist - LinkedList 12= new LinkedList(); + LinkedList 12 = new LinkedList(); 12.einfuegen("Hallo"); 12.einfuegen("Welt");