PUSH PUSH

master
SimonDHG 2023-12-11 16:33:04 +01:00
parent 8fb47e629e
commit a4c2f13086
2 changed files with 111 additions and 0 deletions

101
Set.java Normal file
View File

@ -0,0 +1,101 @@
public class Set<T>
{
private Node<T> first;
private int length;
private Node<T> last;
/**
* Konstruktor, legt leeres Set an
*/
public Set(){
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(){
return this.length;
}
public T get (int n){
Node<T> 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<T> getNode (int n){
Node<T> 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){
}
public boolean contains (T val){
Node<T> current = this.first;
while(current != null){
if(current.wert == val) return true;
current = current.next;
}
return false;
}
public void remove(T val){
Node<T> current = this.first;
int count = 0;
while (current != null){
if (current.next.wert == val){
this.remove(count);
}
count++;
}
}
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;
}
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;
}
/*public String toString(){
Node <T> current = this.first;
String tmp = "";
while (current != null){
tmp = tmp + (String)current.wert;
current = current.next;
}
return tmp;
}*/
}

View File

@ -32,4 +32,14 @@ public class test
l.remove(0);
System.out.println(l.get(0));
}
public static void testset(){
Set<Integer> test = new Set<Integer>();
test.add(1);
test.add(2);
test.add(3);
System.out.println(test);
test.remove(2);
System.out.println(test);
}
}