From 75eece49acf1b37d13bbd9f1c7ff5396e224e3e5 Mon Sep 17 00:00:00 2001 From: hertero Date: Tue, 30 Aug 2022 12:08:19 +0200 Subject: [PATCH] add(i,e)-Methode implementiert --- .../src/solution/SimpleLinkedList.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/SimpleLinkedList/src/solution/SimpleLinkedList.java b/SimpleLinkedList/src/solution/SimpleLinkedList.java index cb4d908..6136ae4 100644 --- a/SimpleLinkedList/src/solution/SimpleLinkedList.java +++ b/SimpleLinkedList/src/solution/SimpleLinkedList.java @@ -125,4 +125,29 @@ public class SimpleLinkedList extends AbstractSimpleList { } 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 newNode = new Node<>(e); + + Node tempNode = getIndex(i); + Node preTempNode = getIndex(i).getPredecessor(); + + preTempNode.setSuccessor(newNode); + newNode.setPredecessor(preTempNode); + newNode.setSuccessor(tempNode); + tempNode.setPredecessor(newNode); + } + } }