81 lines
1.6 KiB
Java
81 lines
1.6 KiB
Java
|
|
/**
|
|
* Beschreiben Sie hier die Klasse Menge.
|
|
*
|
|
* @author (Ihr Name)
|
|
* @version (eine Versionsnummer oder ein Datum)
|
|
*/
|
|
public class Menge<T>
|
|
{
|
|
public Node<T> first;
|
|
public Menge(){
|
|
}
|
|
|
|
public boolean contains(T n){
|
|
Node<T> current = first;
|
|
while (current != null){
|
|
if(current.wert == n){
|
|
return true;
|
|
}
|
|
|
|
}
|
|
return false;
|
|
}
|
|
public void add (T neu) {
|
|
Node<T> n = new Node<T>(neu);//Neue Node mit Zahl "neu " anlegen
|
|
|
|
if(contains(neu) == false){//Überprüfe ob die liste leer ist
|
|
if(first == null){
|
|
//setze neue node als erster Eintrag
|
|
first = n;
|
|
}
|
|
else {
|
|
Node<T> current2 = first;
|
|
|
|
while(current2.next != null){
|
|
current2 = current2.next;
|
|
}
|
|
current2.setNext(n);
|
|
}
|
|
|
|
//current ist jetzt der letzte Eintrag
|
|
//setze neue Node als Nachfolger von bisher letztem Eintrag
|
|
}
|
|
}
|
|
|
|
public void remove(T neu){
|
|
Node<T> n = new Node<T>(neu);
|
|
Node<T> current = first;
|
|
while(current.next.wert != n){
|
|
current = current.next;
|
|
}
|
|
current.next = current.next.next;
|
|
}
|
|
public Menge<T> intersection(Menge<T> s){
|
|
Menge<T> neu = new Menge<T>();
|
|
Node<T> current = first;
|
|
while(current != null){
|
|
current = current.next;
|
|
if(s.contains(current.wert) == true){
|
|
neu.add(current.wert);
|
|
|
|
}
|
|
}
|
|
return neu;
|
|
}
|
|
public Menge<T> union(Menge<T> s){
|
|
Menge<T> neu = new Menge<T>();
|
|
Node<T> current = first;
|
|
while(current != null){
|
|
neu.add(current.wert);
|
|
current = current.next;
|
|
}
|
|
Node<T> current2 = first;
|
|
while(current != null){
|
|
neu.add(current2.wert);
|
|
current = current.next;
|
|
}
|
|
return neu;
|
|
}
|
|
}
|