package list.dl; import java.util.NoSuchElementException; import list.dl.algo.IDLAlgo; import list.dl.algo.IAlgoLeft; import list.dl.algo.IAlgoRight; /** * Models doubly-linked list. * @author DXN * @version 3.1 * @since 10/22/99 */ public class DLList { /** @SBGen Variable (,left Node,,64) */ private ANode _leftNode; /** @SBGen Variable (,right Node,,64) */ private ANode _rightNode; // Data invariant: _leftNode and _rightNode are not null, // and if they are not the end nodes, correctly point back to this DLList. public DLList () { _leftNode = LeftEndNode.Singleton; _rightNode = RightEndNode.Singleton; } /** * Returns the first element on the left. */ public final Object leftFirst() { return _leftNode.leftFirst (this); } /** * Returns the first element on the right. */ public final Object rightFirst () { return _rightNode.rightFirst (this); } /** * Returns the left tail list. */ public final DLList leftRest () { return _leftNode.leftRest (this); } /** * Returns the right tail list. */ public final DLList rightRest() { return _rightNode.rightRest (this); } /** * Inserts a data element to the front of the left list. * @param dat the element to be inserted to the left front. */ public final void insertLeftFront (Object dat) { _leftNode.insertLeftFront (dat, this); } /** * Inserts a data element to the front of the right list. * @param dat the element to be inserted to the left front. */ public final void insertRightFront(Object dat) { _rightNode.insertRightFront (dat, this); } /** * Removes and returns the first element from the left list. */ public final Object remLeftFirst () { return _leftNode.remLeftFirst (this); } /** * Removes and returns the first element from the right list. */ public final Object remRightFirst () { return _rightNode.remRightFirst (this); } /** * Replaces the current first element of the left list with the given data element. */ public final void setLeftFirst(Object dat) { _leftNode.setLeftFirst (dat, this); } /** * Replaces the current first element of the right list with the given data element. */ public final void setRightFirst (Object dat) { _rightNode.setRightFirst (dat, this); } /** * Replaces the current tail of the left list with the given list. * @param dllist the new tail that is to replace the current one. */ public final void setLeftRest(DLList dllist) { _leftNode.setLeftRest (dllist, this); } /** * Replaces the current tail of the right list with the given list. * @param dllist the new tail that is to replace the current one. */ public final void setRightRest(DLList dllist) { _rightNode.setRightRest (dllist, this); } /** * Hook to execute any DLList visitor. * @param algo an algorithm as a DLList visitor. * @param input the input for the algo visitor. */ public Object execute (IDLAlgo algo, Object input) { return algo.visit (this, input); } /** * @param algo * @param input */ public Object executeLeft (IAlgoLeft algo, Object input) { return _leftNode.executeLeft (algo, input, this); } /** * @param algo * @param input */ public Object executeRight (IAlgoRight algo, Object input) { return _rightNode.executeRight (algo, input, this); } /* * @pre null != leftNode, null != rightNode. */ DLList (ANode leftNode, ANode rightNode) { _leftNode = leftNode; _leftNode.setRightParent (this); _rightNode = rightNode; _rightNode.setLeftParent (this); } /** * @param newHead */ final void setLeftNode (ANode newHead) { _leftNode = newHead; _leftNode.setRightParent (this); } /** * @param newHead */ final void setRightNode (ANode newHead) { _rightNode = newHead; _rightNode.setLeftParent (this); } final ANode leftNode () { return _leftNode; } final ANode rightNode () { return _rightNode; } }