From e781629e78b1903a9e6a31daaaea9f111f8c905d Mon Sep 17 00:00:00 2001 From: SimonDHG <@> Date: Mon, 4 Dec 2023 17:14:27 +0100 Subject: [PATCH] heute zu viel extra arbeit gemacht --- LinkedList.java | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ Node.java | 38 +++++++--------------- test.java | 16 ++++++++++ 3 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 LinkedList.java create mode 100644 test.java diff --git a/LinkedList.java b/LinkedList.java new file mode 100644 index 0000000..332cd72 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,83 @@ + +public class LinkedList +{ + private Node start; + public LinkedList(){ + } + public LinkedList(int[] arr){ + for (int i = 0; i < arr.length; i++){ + this.append(arr[i]); + } + } + public LinkedList(int st){ + start = new Node(st); + } + + public void append(int zahl){ + //anlegen eines neuen Nodes + Node neu = new Node(zahl); + Node current = this.start; + if (this.start == null){ + this.start = neu; + } + else{ + while (current.next != null){ //solange es einen Nachfolger gibt + current = current.next; + } + //current ist jetzt das letzte Element + + // hängt neuen Node an + current.next = neu; + } + } + + public int length(){ + Node current = this.start; + int count= 0; + while(current.next != null){ + current = current.next; + count++; + } + + return count; + } + + public int erste(){ + if (start == null) return 0; + int i = this.start.wert; + this.start = start.next; + return i; + } + + public int getNteZahl(int n){ + Node current = this.start; + for ( int i = 0; i < n ; i++ ){ + if (current == null)return 0; + current = current.next; + } + if (current == null ) return 0; + return current.wert; + + } + + public String toString(){ + String result = ""; + + for (int i = 0; i < this.length(); i++){ + result += this.getNteZahl(i) + ","; + } + return result; + } + + /*public String toString(){ + String result = ""; + + Node current = start; + + while(current != null){ + result += current.wert + ","; + current = current.next; + } + return result; + }*/ +} diff --git a/Node.java b/Node.java index 3f0bcf4..1d8c766 100644 --- a/Node.java +++ b/Node.java @@ -1,33 +1,17 @@ -/** - * Beschreiben Sie hier die Klasse Node. - * - * @author (Ihr Name) - * @version (eine Versionsnummer oder ein Datum) - */ public class Node { - // Instanzvariablen - ersetzen Sie das folgende Beispiel mit Ihren Variablen - private int x; - - /** - * Konstruktor für Objekte der Klasse Node - */ - public Node() - { - // Instanzvariable initialisieren - x = 0; + public int wert; + public Node next; + public Node(int w){ + this.wert = w; } - - /** - * Ein Beispiel einer Methode - ersetzen Sie diesen Kommentar mit Ihrem eigenen - * - * @param y ein Beispielparameter für eine Methode - * @return die Summe aus x und y - */ - public int beispielMethode(int y) - { - // tragen Sie hier den Code ein - return x + y; + public void setWert(int w){ + this.wert = w; + } + + public void setNext (Node n){ + this.next = n; + } } diff --git a/test.java b/test.java new file mode 100644 index 0000000..f3b0fa5 --- /dev/null +++ b/test.java @@ -0,0 +1,16 @@ +import java.util.Random; +public class test +{ + public static void test1(){ + Random r = new Random(); + int[] arr = new int[5]; + for (int i = 0; i < arr.length; i++){ + arr[i] = r.nextInt(); + } + LinkedList test = new LinkedList(arr); + + System.out.println(test); + System.out.println(test.erste()); + System.out.println(test); + } +}