From 6636c36298d987a6aed44f504e4035ddd21cc334 Mon Sep 17 00:00:00 2001 From: SimonDHG <@> Date: Tue, 5 Dec 2023 12:08:10 +0100 Subject: [PATCH] hilfe --- LinkedList.java | 32 ++++++++++++++++---------------- Node.java | 8 ++++---- test.java | 14 +++++++++++++- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/LinkedList.java b/LinkedList.java index 332cd72..369c772 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -1,22 +1,22 @@ -public class LinkedList +public class LinkedList { - private Node start; + private Node start; public LinkedList(){ } - public LinkedList(int[] arr){ + public LinkedList(T[] arr){ for (int i = 0; i < arr.length; i++){ this.append(arr[i]); } } - public LinkedList(int st){ + public LinkedList(T st){ start = new Node(st); } - public void append(int zahl){ + public void append(T zahl){ //anlegen eines neuen Nodes - Node neu = new Node(zahl); - Node current = this.start; + Node neu = new Node(zahl); + Node current = this.start; if (this.start == null){ this.start = neu; } @@ -34,7 +34,7 @@ public class LinkedList public int length(){ Node current = this.start; int count= 0; - while(current.next != null){ + while(current != null){ current = current.next; count++; } @@ -42,20 +42,20 @@ public class LinkedList return count; } - public int erste(){ - if (start == null) return 0; - int i = this.start.wert; + public T erste(){ + if (start == null) return null; + T i = this.start.wert; this.start = start.next; return i; } - public int getNteZahl(int n){ - Node current = this.start; + public T getNtenWert(int n){ + Node current = this.start; for ( int i = 0; i < n ; i++ ){ - if (current == null)return 0; + if (current == null)return null; current = current.next; } - if (current == null ) return 0; + if (current == null ) return null; return current.wert; } @@ -64,7 +64,7 @@ public class LinkedList String result = ""; for (int i = 0; i < this.length(); i++){ - result += this.getNteZahl(i) + ","; + result += this.getNtenWert(i) + ","; } return result; } diff --git a/Node.java b/Node.java index 1d8c766..5b3a876 100644 --- a/Node.java +++ b/Node.java @@ -1,12 +1,12 @@ -public class Node +public class Node { - public int wert; + public T wert; public Node next; - public Node(int w){ + public Node(T w){ this.wert = w; } - public void setWert(int w){ + public void setWert(T w){ this.wert = w; } diff --git a/test.java b/test.java index f3b0fa5..054af57 100644 --- a/test.java +++ b/test.java @@ -7,10 +7,22 @@ public class test for (int i = 0; i < arr.length; i++){ arr[i] = r.nextInt(); } - LinkedList test = new LinkedList(arr); + LinkedList test = new LinkedList(arr); System.out.println(test); System.out.println(test.erste()); System.out.println(test); + System.out.println(test.getNtenWert(2)); + System.out.println(test.getNtenWert(23)); + + LinkedList test2 = new LinkedList([{"Hallo", "Welt"}]); + + + } + public static void test2(){ + Node n = new Node("Hallo"); + Node i = new Node(5); + + } }