From a137bfc354528e0e9f50627f7d7e458c8d6d8795 Mon Sep 17 00:00:00 2001 From: david <@> Date: Wed, 11 Jan 2023 11:10:41 +0100 Subject: [PATCH] Ubgrade 11.1 --- LinkedList.java | 90 +++++++++++++++++++++++++++++++++++++------------ Node.java | 6 ++-- test.java | 15 ++++++--- 3 files changed, 82 insertions(+), 29 deletions(-) diff --git a/LinkedList.java b/LinkedList.java index 14a0410..ab7afe9 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -5,31 +5,79 @@ * @author (Ihr Name) * @version (eine Versionsnummer oder ein Datum) */ -public class LinkedList +public class LinkedList { - public Node first; - public LinkedList(){ + public Node first; + public LinkedList(){ } - - public void einfuegen (int neu) { - Node n = new Node(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; + + public void einfuegen (T neu) { + Node n = new Node(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 { + 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 } - else { + + public int laenge(){ Node current = first; - - while(current.next != null){ - current = current.next; + int laenge = 0; + while(current != null){ + current = current.next; + laenge++; + } + return laenge; + } - current.setNext(n); + + 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){ + neu.setNext(first); + first = neu; + }else{ + Node current = first; + //gehe an den Vorgänger des zu löschenden Eintrags + for(int i=0; i { -public int zahl; +public T zahl; public Node next; -public Node(int z){ +public Node(T z){ zahl = z; } public void setNext(Node n){ diff --git a/test.java b/test.java index a477b1f..5823dbe 100644 --- a/test.java +++ b/test.java @@ -7,8 +7,10 @@ */ public class test { - public void test(){ - + public static void test(){ + Node test = new Node("Hallo"); + Node test2 = new Node(5); + LinkedList liste = new LinkedList(); liste.einfuegen(5); liste.einfuegen(6); @@ -16,7 +18,8 @@ public class test liste.einfuegen(3); liste.einfuegen(7); liste.einfuegen(9); - + liste.loesche(2); + liste.hinzufügen(8,"wirklich"); Node current = liste.first; while(current != null){ @@ -24,8 +27,10 @@ public class test current = current.next; } + System.out.println("Die Liste hat "+liste.laenge()+" Einträge. "); - - + + } + }