diff --git a/List.java b/List.java index f1270d8..b8d20a2 100644 --- a/List.java +++ b/List.java @@ -94,6 +94,31 @@ public class List } public T remove(int n) { - return null; //Platzhalter + if (n >= size()) return null; //um fehler zu beheben: Parameter darf nicht größer als tatsächlicher Länge sein + + if ( n == 0){ // wenn Parameter 0 ist dann... + T tmp = first.wert; // wert von first (=0) speichern + first = first.next; //first "pfeil" auf den nächsten + return tmp;//wert ausgeben + } + Node current = first; + + for (int i = 0; i < n-1; i++){ + current = current.next; + } + + T tmp = current.next.wert; // (Zwischenvariable um gelöschten Wert zu speichern) + current.next = current.next.next; // Pfeil auf nächsten verschieben damit er nichr auf gelöschtem Zeigt + return tmp; //gelöschten Wert ausgeben + } + + public String toString(){ // glaub um halt alles schön auszugeben + String result = ""; + Node current = first; + while (current != null){ + result += current.wert + ", "; + current = current.next; + } + return result; + } } -} diff --git a/Node2.java b/Node2.java deleted file mode 100644 index 22356fb..0000000 --- a/Node2.java +++ /dev/null @@ -1,15 +0,0 @@ - -/** - * 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/Queue.java b/Queue.java new file mode 100644 index 0000000..0f039e9 --- /dev/null +++ b/Queue.java @@ -0,0 +1,79 @@ + +/** + * Beschreiben Sie hier die Klasse List_T_. + * + * @author (Ihr Name) + * @version (eine Versionsnummer oder ein Datum) + */ +public class Queue +{ + private Node first; + + public Queue() { + 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 front(int n) { + + if (this.first == null) return null; + return this.first.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 boolean contains(T val) { + Node current = first; + + while (current != null) { + if (current.wert.equals(val)) return true; + } + + return false; + } + + public T dequeue() { + T tmp = first.next.wert; // (Zwischenvariable um gelöschten Wert zu speichern) + first.next = first.next.next; // Pfeil auf nächsten verschieben damit er nichr auf gelöschtem Zeigt + return tmp; //gelöschten Wert ausgeben + } + + public String toString(){ // glaub um halt alles schön auszugeben + String result = ""; + Node current = first; + while (current != null){ + result += current.wert + ", "; + current = current.next; + } + return result; + } + } diff --git a/Test.java b/Test.java index fb954ab..c0ebb3c 100644 --- a/Test.java +++ b/Test.java @@ -25,4 +25,5 @@ public class Test return n1; } + } diff --git a/Test2.java b/Test2.java index 1499585..ec10efa 100644 --- a/Test2.java +++ b/Test2.java @@ -26,8 +26,8 @@ public class Test2 System.out.println(l.getNteZahl(23)); // gibt 0 aus weil Liste nicht so lang ist LinkedList 12 = new LinkedList(); - 12.einfuegen("Hallo"); - 12.einfuegen("Welt"); + 12.add("Hallo"); + 12.add("Welt"); System.out.println(12); }