/*=============================================================================
 *     Copyright Rob Clark 2000.  All Rights Reserved.
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
 */


/**
 * The list class constructor.
 * 
 * @author Rob Clark
 * <!--$Format: " * @version $Revision$"$-->
 * @version 1.2
 */
public function List()
{
  private var head = null;
  private var tail;

  /**
   * Add an object to the list.
   * 
   * @param obj          what to add to the list
   */
  public function add( obj )
  {
    if( head == null )
    {
      head = new ListNode(obj);
      tail = head;
    }
    else
    {
      tail.setNext( new ListNode(obj) );
      tail = tail.getNext();
    }
  }

  /**
   * Remove an object from the list.  The first occurance of this object is
   * removed.
   * 
   * @param obj          the object to remove
   * @return <code>true</code> if matching object found and removed, else 
   * <code>false</code> if not found
   */
  public function remove( obj )
  {
    var node;
    var prev;

    for( node = head, prev = null;
         node != null;
         prev = node, node = node.getNext() )
    {
      if( obj == node.getObj() )
      {
        if( prev == null )
          head = node.getNext();
        else
          prev.setNext( node.getNext() );

        return true;
      }
    }

    return false;
  }

  /**
   * Return the contents of the list as an array.
   * 
   * @return an array
   */
  public function toArray()
  {
    var arr = [];

    var i=0;
    for( var node = head;
         node != null;
         node = node.getNext(), i++ )
    {
      arr[i] = node.getObj();
    }

    return arr;
  }


  /**
   * Inner-class implementing a list node.
   * 
   * @param obj          the data
   */
  private function ListNode( obj )
  {
    var next = null;

    public function getObj()
    {
      return obj;
    }

    public function getNext()
    {
      return next;
    }

    public function setNext( next )
    {
      this.next = next;
    }
  }
}




/*
 *   Local Variables:
 *   tab-width: 2
 *   indent-tabs-mode: nil
 *   mode: java
 *   c-indentation-style: java
 *   c-basic-offset: 2
 *   eval: (c-set-offset 'statement-cont '0)
 *   eval: (c-set-offset 'substatement-open '0)
 *   eval: (c-set-offset 'case-label '+)
 *   eval: (c-set-offset 'inclass '+)
 *   eval: (c-set-offset 'inline-open '0)
 *   End:
 */