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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue