From f470c248c766f1fc22de3a5255a508c7509ee163 Mon Sep 17 00:00:00 2001 From: David <@> Date: Mon, 16 Jan 2023 12:52:09 +0100 Subject: [PATCH] Ubgrade 16.1.2023 --- LinkedList2.java | 71 ----------------------------------------- List.java | 82 ++++++++++++++++++++++++++++++++++++++++++------ Node.java | 2 +- test.java | 28 ++--------------- 4 files changed, 75 insertions(+), 108 deletions(-) delete mode 100644 LinkedList2.java diff --git a/LinkedList2.java b/LinkedList2.java deleted file mode 100644 index dd6a496..0000000 --- a/LinkedList2.java +++ /dev/null @@ -1,71 +0,0 @@ - -/** - * Beschreiben Sie hier die Klasse List. - * - * @author (Ihr Name) - * @version (eine Versionsnummer oder ein Datum) - */ -public class LinkedList2 -{ - public List first; - - public LinkedList2(){} - public void einfuegen (T neu) { - List n = new List(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 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 current = first; - for(int i = 0; i current = first; - - //gehe an den Vorgänger des zu löschenden Eintrags - for(int i=0; i { - -public T zahl; + public Node first; + public List(){ + } -public List next; + public void einfuegen (T neu) { + Node n = new Node(neu);//Neue Node mit Zahl "neu " anlegen -public List(T g){ -zahl = g; -} -public void setNext(List h){ -next = h; + //Überprüfe ob die liste leer ist + if(first == null){ + //setze neue node als erster Eintrag + first = n; + } + else { + Node 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 int laenge(){ + Node current = first; + int laenge = 0; + while(current != null){ + current = current.next; + laenge++; + } + return laenge; + + } + + public T getNteZahl(int n){ + Node current = first; + for(int i = 0; i current = first; + + //gehe an den Vorgänger des zu löschenden Eintrags + for(int i=0; i neu = new Node(wert); + if(n==0){ + //setze Nachfolger auf bisherigen Stand + neu.setNext(first); + //setze neuen Start auf neuen + first = neu; + }else{ + Node current = first; + //gehe an den Vorgänger des zu löschenden Eintrags + for(int i=0; i public T zahl; -public Node next; +public Node next; public Node(T z){ zahl = z; diff --git a/test.java b/test.java index 8bb41ba..f0a4a62 100644 --- a/test.java +++ b/test.java @@ -11,7 +11,7 @@ public class test Node test = new Node("Hallo"); Node test2 = new Node(5); - LinkedList liste = new LinkedList(); + List liste = new List(); liste.einfuegen(5); liste.einfuegen(6); liste.einfuegen(8); @@ -19,7 +19,7 @@ public class test liste.einfuegen(7); liste.einfuegen(9); liste.loesche(2); - liste.hinzufügen(8,"wirklich"); + liste.hinzufügen(4,"wirklich"); Node current = liste.first; while(current != null){ @@ -32,29 +32,5 @@ public class test } - public static void test2(){ - List test = new List("Hallo"); - List test2 = new List(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. "); - - - -} }