From e2bf8c726e4ce840b5d524463426098c11431b40 Mon Sep 17 00:00:00 2001 From: SimonDHG <@> Date: Mon, 11 Dec 2023 17:22:14 +0100 Subject: [PATCH] SOS wir haben eine contains Methode --- Set.java | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Set.java b/Set.java index 9d44c68..7d524fb 100644 --- a/Set.java +++ b/Set.java @@ -48,10 +48,16 @@ public class Set } public void add (T val){ - - - - + if (!this.contains(val)){ + Node added = new Node(val); + if (this.first == null){ + this.first = added; + this.last = added; + } + else{ this.last.next = added; + this.last = added; + } + } } public boolean contains (T val){ @@ -64,31 +70,25 @@ public class Set } public void remove(T val){ + if(!this.contains(val))return; Node current = this.first; - int count = 0; - while (current != null){ - if (current.next.wert == val){ - this.remove(count); - } - count++; + while(current.next.wert != val){ + current = current.next; } + current.next.next = current.next; } - - private 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; + + public Set intersection(Set s){ + Set tmp = new Set(); + Node current = this.first; + while(current != null){ + if (s.contains(current.wert)) tmp.add(current.wert); + current=current.next; } - 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; + return tmp; } + /*public String toString(){ Node current = this.first; String tmp = "";