Browse Source

Anpassung add-/remove-/contains-/add(i,e)-Methoden

vollständige_und_weitere_testate
hertero 3 years ago
committed by chris
parent
commit
08f8538890
  1. 61
      SimpleLinkedList/src/solution/SimpleLinkedList.java

61
SimpleLinkedList/src/solution/SimpleLinkedList.java

@ -1,9 +1,5 @@
package solution; package solution;
//2017_OOP_90.jar importieren die von Bayer in der Aufgabenstellung zur Verfügung gestellt wird.
//import testat.*;
public class SimpleLinkedList<E> extends AbstractSimpleList<E> { public class SimpleLinkedList<E> extends AbstractSimpleList<E> {
Node<E> firstNode; Node<E> firstNode;
@ -23,20 +19,21 @@ public class SimpleLinkedList<E> extends AbstractSimpleList<E> {
public boolean add(E e) { public boolean add(E e) {
if (e == null) { if (e == null) {
return false; return false;
} else if (firstNode == null) {
firstNode = new Node<E>(e);
} else { } else {
Node<E> newNode = new Node<>(e); Node<E> currentNode = firstNode;
while (currentNode != null) {
if (firstNode == null) { if (currentNode.getSuccessor() == null) {
firstNode = newNode; break;
} else {
Node<E> currentNode = firstNode;
while (currentNode != null) {
currentNode = currentNode.getSuccessor();
} }
currentNode.setSuccessor(newNode); currentNode = currentNode.getSuccessor();
currentNode.setPredecessor(currentNode);
} }
Node<E> newNode = new Node<E>(e);
currentNode.setSuccessor(newNode);
newNode.setPredecessor(currentNode);
} }
size++; size++;
return true; return true;
} }
@ -48,14 +45,16 @@ public class SimpleLinkedList<E> extends AbstractSimpleList<E> {
} }
Node<E> currentNode = firstNode; Node<E> currentNode = firstNode;
while (currentNode != null) { while (currentNode != null) {
currentNode = currentNode.getSuccessor(); if (o.equals(currentNode.getPayload())) {
} currentNode.setPayload(null);
if (o.equals(currentNode)) { size--;
currentNode.setPayload(null); return true;
size--; }
currentNode = currentNode.getPredecessor();
return true;
if (currentNode.getSuccessor() == null) {
break;
}
currentNode = currentNode.getSuccessor();
} }
return false; return false;
} }
@ -67,11 +66,13 @@ public class SimpleLinkedList<E> extends AbstractSimpleList<E> {
} }
Node<E> currentNode = firstNode; Node<E> currentNode = firstNode;
while (currentNode != null) { while (currentNode != null) {
if (o.equals(currentNode.getPayload())) {
return true;
}
currentNode = currentNode.getSuccessor(); currentNode = currentNode.getSuccessor();
} }
if (o.equals(currentNode)) {
return true;
}
return false; return false;
} }
@ -133,16 +134,14 @@ public class SimpleLinkedList<E> extends AbstractSimpleList<E> {
} }
set(0, e); set(0, e);
} else if (size > 0) { } else if (i == size) {
if (i == size) { add(e);
add(e);
}
} else if (size > 0 && i < size) { } else if (size > 0 && i < size) {
Node<E> newNode = new Node<>(e); Node<E> newNode = new Node<>(e);// node3
Node<E> tempNode = getIndex(i); Node<E> tempNode = getIndex(i); // node2
Node<E> preTempNode = getIndex(i).getPredecessor(); Node<E> preTempNode = getIndex(i).getPredecessor(); // node1
preTempNode.setSuccessor(newNode); preTempNode.setSuccessor(newNode);
newNode.setPredecessor(preTempNode); newNode.setPredecessor(preTempNode);

Loading…
Cancel
Save