diff --git a/List.java b/List.java new file mode 100644 index 0000000..f442b8f --- /dev/null +++ b/List.java @@ -0,0 +1,102 @@ + +public class List +{ + private Node first; + private int length; + private Node last; + /** + * Konstruktor, legt leere Liste an + */ + public List(){ + this.first = null; + length = 0; + this.last= null; + } + + /** + * Gibt zurück, ob die Liste leer ist (true heißt leer) + */ + public boolean isEmpty(){ + return this.first == null; + } + + /** + * Gibt die Länge der Liste zurück + */ + public int size(){ + Node current = this.first; + int count= 0; + while(current != null){ + current = current.next; + count++; + } + + return length; + } + + public T get (int n){ + Node current = this.first; + for ( int i = 0; i < n ; i++ ){ + if (current == null)return null; + current = current.next; + } + if (current == null ) return null; + return current.wert; + } + + private Node getNode (int n){ + Node current = this.first; + for ( int i = 0; i < n ; i++ ){ + if (current == null)return null; + current = current.next; + } + if (current == null ) return null; + return current; + } + + public void add (T val){ + Node added = new Node(val); + Node current = this.first; + if (this.first == null){ + this.first = added; + this.last = added; + } + else{ + this.last.next = added; + this.last = added; + } + this.length++; + } + + public void add (int n, T val){ + Node added = new Node(val); + if(this.getNode(n-1) == null)this.add(val); + added.next = this.getNode(n-1).next; + this.getNode(n-1).next = added; + length++; + } + + public boolean contains (T val){ + Node current = this.first; + while(current != null){ + if(current.wert == val) return true; + current = current.next; + } + return false; + } + + public T remove(int n){ + if (this.length <= n ) return null; + if ( n == 0) { + T ret = this.first.wert; + this.first = this.first.next; + this.length--; + return ret; + } + T ret = getNode(n).wert; + if (getNode(n-1).next.next == null) this.last = getNode(n-1); + getNode(n-1).next = getNode(n-1).next.next; + length--; + return ret; + } +} diff --git a/Queue.java b/Queue.java new file mode 100644 index 0000000..237a5bd --- /dev/null +++ b/Queue.java @@ -0,0 +1,37 @@ + +public class Queue +{ + private Node first; + private Node last; + + public Queue(){ + this.first = null; + this.last = null; + } + + public boolean isEmpty(){ + return this.first == null; + } + public void enqueue(T val){ + Node added = new Node(val); + Node current = this.first; + if (this.first == null){ + this.first = added; + this.last = added; + } + else{ + this.last.next = added; + this.last = added; + } + } + public T dequeue (){ + if (this.first == null) return null; + T ret = this.first.wert; + this.first = this.first.next; + return ret; + } + public T front (){ + if (this.first == null) return null; + return this.first.wert; + } +} diff --git a/Stack.java b/Stack.java new file mode 100644 index 0000000..697a1c8 --- /dev/null +++ b/Stack.java @@ -0,0 +1,27 @@ + +public class Stack +{ + private Node first; + + public Stack(){ + this.first = null; + } + public boolean isEmpty(){ + return this.first == null; + } + public void push(T val){ + Node added = new Node(val); + added.next = this.first; + this.first = added; + } + public T pop (){ + if (this.first == null) return null; + T ret = this.first.wert; + this.first = this.first.next; + return ret; + } + public T top (){ + if (this.first == null) return null; + return this.first.wert; + } +} diff --git a/test.java b/test.java index 054af57..07ef3fa 100644 --- a/test.java +++ b/test.java @@ -15,7 +15,7 @@ public class test System.out.println(test.getNtenWert(2)); System.out.println(test.getNtenWert(23)); - LinkedList test2 = new LinkedList([{"Hallo", "Welt"}]); + //LinkedList test2 = new LinkedList([{"Hallo", "Welt"}]); } @@ -25,4 +25,11 @@ public class test } + public static void testList(){ + List l = new List(); + l.add(1); + l.add(3); + l.remove(0); + System.out.println(l.get(0)); + } }