Ubgrade 13.1.2023
parent
a137bfc354
commit
dfbc01fc9b
|
@ -67,7 +67,9 @@ public class LinkedList<T>
|
|||
|
||||
Node<T> neu = new Node<T>(wert);
|
||||
if(n==0){
|
||||
//setze Nachfolger auf bisherigen Stand
|
||||
neu.setNext(first);
|
||||
//setze neuen Start auf neuen
|
||||
first = neu;
|
||||
}else{
|
||||
Node<T> current = first;
|
||||
|
@ -80,4 +82,5 @@ public class LinkedList<T>
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
/**
|
||||
* Beschreiben Sie hier die Klasse List.
|
||||
*
|
||||
* @author (Ihr Name)
|
||||
* @version (eine Versionsnummer oder ein Datum)
|
||||
*/
|
||||
public class List<T>
|
||||
{
|
||||
|
||||
public T zahl;
|
||||
|
||||
public List next;
|
||||
|
||||
public List(T g){
|
||||
zahl = g;
|
||||
}
|
||||
public void setNext(List h){
|
||||
next = h;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
24
test.java
24
test.java
|
@ -32,5 +32,29 @@ public class test
|
|||
|
||||
|
||||
}
|
||||
public static void test2(){
|
||||
List<String> test = new List<String>("Hallo");
|
||||
List<Integer> test2 = new List<Integer>(5);
|
||||
|
||||
LinkedList2 liste = new LinkedList2();
|
||||
liste.einfuegen(5);
|
||||
liste.einfuegen(6);
|
||||
liste.einfuegen(8);
|
||||
liste.einfuegen(3);
|
||||
liste.einfuegen(7);
|
||||
liste.einfuegen(9);
|
||||
liste.loesche(2);
|
||||
|
||||
List current = liste.first;
|
||||
|
||||
while(current != null){
|
||||
System.out.println( current.zahl );
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
System.out.println("Die Liste hat "+liste.laenge()+" Einträge. ");
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue