Add method for inserting nodes
This commit is contained in:
parent
83700823d0
commit
0aeb39d677
1 changed files with 16 additions and 3 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue