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.2 * @since 10/17/00 */ 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 getLeftFirst() { return _leftNode.getFirst (this); } /** * Returns the first element on the right. */ public final Object getRightFirst () { return _rightNode.getFirst (this); } /** * Returns the left tail list. */ public final DLList getLeftRest () { return _leftNode.getLeftRest (this); } /** * Returns the right tail list. */ public final DLList getRightRest() { return _rightNode.getRightRest (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 remLeftFront () { return _leftNode.remLeftFront (this); } /** * Removes and returns the first element from the right list. */ public final Object remRightFront () { return _rightNode.remRightFront (this); } /** * Replaces the current first element of the left list with the given data element. */ public final void setLeftFirst(Object dat) { _leftNode.setFirst (dat, this); } /** * Replaces the current first element of the right list with the given data element. */ public final void setRightFirst (Object dat) { _rightNode.setFirst (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.setRightOwner (this); _rightNode = rightNode; _rightNode.setLeftOwner (this); } /** * @param newHead */ final void setLeftNode (ANode newHead) { _leftNode = newHead; _leftNode.setRightOwner (this); } /** * @param newHead */ final void setRightNode (ANode newHead) { _rightNode = newHead; _rightNode.setLeftOwner (this); } final ANode getLeftNode () { return _leftNode; } final ANode getRightNode () { return _rightNode; } }