From 08f853889033e40d912b9284379f3ed71a83a031 Mon Sep 17 00:00:00 2001 From: hertero Date: Thu, 1 Sep 2022 16:34:05 +0200 Subject: [PATCH] Anpassung add-/remove-/contains-/add(i,e)-Methoden --- .../src/solution/SimpleLinkedList.java | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/SimpleLinkedList/src/solution/SimpleLinkedList.java b/SimpleLinkedList/src/solution/SimpleLinkedList.java index 6136ae4..ea4bc75 100644 --- a/SimpleLinkedList/src/solution/SimpleLinkedList.java +++ b/SimpleLinkedList/src/solution/SimpleLinkedList.java @@ -1,9 +1,5 @@ package solution; -//2017_OOP_90.jar importieren die von Bayer in der Aufgabenstellung zur Verfügung gestellt wird. - -//import testat.*; - public class SimpleLinkedList extends AbstractSimpleList { Node firstNode; @@ -23,20 +19,21 @@ public class SimpleLinkedList extends AbstractSimpleList { public boolean add(E e) { if (e == null) { return false; + } else if (firstNode == null) { + firstNode = new Node(e); } else { - Node newNode = new Node<>(e); - - if (firstNode == null) { - firstNode = newNode; - } else { - Node currentNode = firstNode; - while (currentNode != null) { - currentNode = currentNode.getSuccessor(); + Node currentNode = firstNode; + while (currentNode != null) { + if (currentNode.getSuccessor() == null) { + break; } - currentNode.setSuccessor(newNode); - currentNode.setPredecessor(currentNode); + currentNode = currentNode.getSuccessor(); } + Node newNode = new Node(e); + currentNode.setSuccessor(newNode); + newNode.setPredecessor(currentNode); } + size++; return true; } @@ -48,14 +45,16 @@ public class SimpleLinkedList extends AbstractSimpleList { } Node currentNode = firstNode; while (currentNode != null) { - currentNode = currentNode.getSuccessor(); - } - if (o.equals(currentNode)) { - currentNode.setPayload(null); - size--; - currentNode = currentNode.getPredecessor(); - return true; + if (o.equals(currentNode.getPayload())) { + currentNode.setPayload(null); + size--; + return true; + } + if (currentNode.getSuccessor() == null) { + break; + } + currentNode = currentNode.getSuccessor(); } return false; } @@ -67,11 +66,13 @@ public class SimpleLinkedList extends AbstractSimpleList { } Node currentNode = firstNode; while (currentNode != null) { + if (o.equals(currentNode.getPayload())) { + return true; + } + currentNode = currentNode.getSuccessor(); } - if (o.equals(currentNode)) { - return true; - } + return false; } @@ -133,16 +134,14 @@ public class SimpleLinkedList extends AbstractSimpleList { } set(0, e); - } else if (size > 0) { - if (i == size) { - add(e); - } + } else if (i == size) { + add(e); } else if (size > 0 && i < size) { - Node newNode = new Node<>(e); + Node newNode = new Node<>(e);// node3 - Node tempNode = getIndex(i); - Node preTempNode = getIndex(i).getPredecessor(); + Node tempNode = getIndex(i); // node2 + Node preTempNode = getIndex(i).getPredecessor(); // node1 preTempNode.setSuccessor(newNode); newNode.setPredecessor(preTempNode);