From 9bb4149c94e199dbeca3f357d51b4a2a3850b1c0 Mon Sep 17 00:00:00 2001 From: Kai Sellmann Date: Tue, 30 May 2023 19:25:19 +0200 Subject: [PATCH] =?UTF-8?q?=C3=9Cbung=20generische=20Liste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sources/src/main/java/pr2/generics/einfach/Liste.java | 8 ++++---- sources/src/main/java/pr2/generics/einfach/ListeNode.java | 7 ++++++- .../main/java/pr2/generics/einfach/test/ListeTest.java | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/sources/src/main/java/pr2/generics/einfach/Liste.java b/sources/src/main/java/pr2/generics/einfach/Liste.java index b4be384..86b395d 100644 --- a/sources/src/main/java/pr2/generics/einfach/Liste.java +++ b/sources/src/main/java/pr2/generics/einfach/Liste.java @@ -8,7 +8,7 @@ package pr2.generics.einfach; /** * Eine einfache, verkettete Liste. */ -public class Liste { +public class Liste { /** Referenz auf den ersten Knoten. */ private ListeNode first; @@ -31,7 +31,7 @@ public class Liste { current = nextNode; } else { - current.next = nextNode; + current.setNext(nextNode); current = nextNode; } } @@ -47,7 +47,7 @@ public class Liste { ListeNode node = first; while ((node != null) && (count < index)) { - node = node.next; + node = node.getNext(); count++; } @@ -78,7 +78,7 @@ public class Liste { ListeNode node = first; while (node != null) { - node = node.next; + node = node.getNext(); count++; } diff --git a/sources/src/main/java/pr2/generics/einfach/ListeNode.java b/sources/src/main/java/pr2/generics/einfach/ListeNode.java index bb1d1ca..0336f11 100644 --- a/sources/src/main/java/pr2/generics/einfach/ListeNode.java +++ b/sources/src/main/java/pr2/generics/einfach/ListeNode.java @@ -10,7 +10,6 @@ class ListeNode { /** Referenz auf den nächsten Knoten. */ ListeNode next; - /** * Legt einen neuen Knoten an. * @@ -19,4 +18,10 @@ class ListeNode { ListeNode(Object data) { this.data = data; } + public ListeNode getNext() { + return next; + } + public void setNext(ListeNode next) { + this.next = next; + } } diff --git a/sources/src/main/java/pr2/generics/einfach/test/ListeTest.java b/sources/src/main/java/pr2/generics/einfach/test/ListeTest.java index e680148..673d13b 100644 --- a/sources/src/main/java/pr2/generics/einfach/test/ListeTest.java +++ b/sources/src/main/java/pr2/generics/einfach/test/ListeTest.java @@ -22,7 +22,7 @@ public class ListeTest { l.add("Hallo"); assertEquals(1, l.size()); - l.add("Hugo"); + l.add(34); assertEquals(2, l.size()); l.add("Peter"); l.add("Alfons");