72 lines
1.6 KiB
Java
72 lines
1.6 KiB
Java
|
|
/**
|
|
* Beschreiben Sie hier die Klasse List.
|
|
*
|
|
* @author (Ihr Name)
|
|
* @version (eine Versionsnummer oder ein Datum)
|
|
*/
|
|
public class LinkedList2<T>
|
|
{
|
|
public List<T> first;
|
|
|
|
public LinkedList2(){}
|
|
public void einfuegen (T neu) {
|
|
List<T> n = new List<T>(neu);//Neue Node mit Zahl "neu " anlegen
|
|
|
|
//Überprüfe ob die liste leer ist
|
|
if(first == null){
|
|
//setze neue node als erster Eintrag
|
|
first = n;
|
|
}
|
|
else {
|
|
List<T> current = first;
|
|
|
|
while(current.next != null){
|
|
current = current.next;
|
|
}
|
|
current.setNext(n);
|
|
}
|
|
|
|
//current ist jetzt der letzte Eintrag
|
|
//setze neue Node als Nachfolger von bisher letztem Eintrag
|
|
}
|
|
public boolean isEmpty(){
|
|
if (first == null){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int laenge(){
|
|
List current = first;
|
|
int laenge = 0;
|
|
while(current != null){
|
|
current = current.next;
|
|
laenge++;
|
|
}
|
|
return laenge;
|
|
}
|
|
|
|
|
|
public T getNteZahl(int n){
|
|
List<T> current = first;
|
|
for(int i = 0; i<n; i++){
|
|
current = current.next;
|
|
}
|
|
return current.zahl;
|
|
}
|
|
|
|
public void loesche(int n){
|
|
List<T> current = first;
|
|
|
|
//gehe an den Vorgänger des zu löschenden Eintrags
|
|
for(int i=0; i<n-1; i++){
|
|
current = current.next;
|
|
}
|
|
//setze Übernächsten Eintrag als Nachfolger
|
|
current.next = current.next.next;
|
|
|
|
}
|
|
|
|
}
|