public class Set { private Node first; private int length; private Node last; /** * Konstruktor, legt leeres Set an */ public Set(){ this.first = null; this.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(){ return this.length; } private 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){ 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; } this.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 void remove(T val){ if(!this.contains(val))return; Node current = this.first; if(this.first.wert == val){ this.first = this.first.next; } while(current.next.wert != val){ current = current.next; } current.next = current.next.next; this.length--; } 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; } return tmp; } public Set unionUnoptimized(Set s){ Set tmp = new Set(); for (int i = 0; i < s.size(); i++){ tmp.add(s.get(i)); } for (int i = 0; i < this.length; i++){ if (!tmp.contains(this.get(i))) tmp.add(this.get(i)); } return tmp; } public Set union(Set s){ Set tmp = new Set(); Node current = s.first; while (current != null){ tmp.add(current.wert); current = current.next; } current = this.first; while (current != null){ tmp.add(current.wert); current = current.next; } return tmp; } public Set difference(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; } return tmp; } public boolean subset(Set s){ Node current = this.first; while (current != null){ if (!s.contains(current.wert)) return false; } return true; } public String toString(){ Node current = this.first; String tmp = ""; while (current != null){ tmp = tmp + current.wert + ", "; current = current.next; } return tmp; } }