From 0aeb39d677f1ff64a3117d9273d41043bcbc33f0 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 28 Sep 2022 09:53:35 +0200 Subject: [PATCH] Add method for inserting nodes --- .../main/java/ch/nuth/zhaw/exbox/MyList.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/MyList.java b/app/src/main/java/ch/nuth/zhaw/exbox/MyList.java index 718011a..1ce3e6e 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/MyList.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/MyList.java @@ -188,11 +188,24 @@ public class MyList extends AbstractList { return anchor; } + /** + * Inserts the specified `item` after the specified `node`. + * + * @param node + * The node to insert the `item` after. + * + * @param item + * The item to insert. + */ + protected void insertAfter(IListNode node, Object item) { + IListNode newNode = new ListNode(item, node, node.getNextNode()); + node.setNextNode(newNode); + newNode.getNextNode().setPreviousNode(newNode); + } + @Override public boolean add(Object e) { - IListNode newNode = new ListNode(e, getAnchor(), getAnchor().getPreviousNode()); - getAnchor().getPreviousNode().setNextNode(newNode); - getAnchor().setPreviousNode(newNode); + insertAfter(getAnchor().getPreviousNode(), e); return true; }