master
SimonDHG 2023-12-05 12:08:10 +01:00
parent e781629e78
commit 6636c36298
3 changed files with 33 additions and 21 deletions

View File

@ -1,22 +1,22 @@
public class LinkedList
public class LinkedList<T>
{
private Node start;
private Node<T> 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<T> neu = new Node(zahl);
Node<T> 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<T> 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;
}

View File

@ -1,12 +1,12 @@
public class Node
public class Node<T>
{
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;
}

View File

@ -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<Integer> 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<String> test2 = new LinkedList<String>([{"Hallo", "Welt"}]);
}
public static void test2(){
Node<String> n = new Node<String>("Hallo");
Node<Integer> i = new Node<Integer>(5);
}
}