Add method for inserting nodes

This commit is contained in:
Manuel Thalmann 2022-09-28 09:53:35 +02:00
parent 83700823d0
commit 0aeb39d677

View file

@ -188,11 +188,24 @@ public class MyList extends AbstractList<Object> {
return anchor; 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 @Override
public boolean add(Object e) { public boolean add(Object e) {
IListNode newNode = new ListNode(e, getAnchor(), getAnchor().getPreviousNode()); insertAfter(getAnchor().getPreviousNode(), e);
getAnchor().getPreviousNode().setNextNode(newNode);
getAnchor().setPreviousNode(newNode);
return true; return true;
} }