Browse Source

add(i,e)-Methode implementiert

vollständige_und_weitere_testate
hertero 3 years ago
committed by chris
parent
commit
75eece49ac
  1. 25
      SimpleLinkedList/src/solution/SimpleLinkedList.java

25
SimpleLinkedList/src/solution/SimpleLinkedList.java

@ -125,4 +125,29 @@ public class SimpleLinkedList<E> extends AbstractSimpleList<E> {
}
return false;
}
public void add(int i, E e) throws IndexOutOfBoundsException {
if (size == 0) {
if (i != 0) {
throw new IndexOutOfBoundsException("Index " + i + " is out of bounds!");
}
set(0, e);
} else if (size > 0) {
if (i == size) {
add(e);
}
} else if (size > 0 && i < size) {
Node<E> newNode = new Node<>(e);
Node<E> tempNode = getIndex(i);
Node<E> preTempNode = getIndex(i).getPredecessor();
preTempNode.setSuccessor(newNode);
newNode.setPredecessor(preTempNode);
newNode.setSuccessor(tempNode);
tempNode.setPredecessor(newNode);
}
}
}

Loading…
Cancel
Save