/*
 * File: GenericNode.java
 * 
 * Date		Author		Changes
 * ----------------------------------------------------------------
 * 19 Feb 1998	M Ravishankar	Created
 */

/**
 * Generic node in a linear linked list.  Users should subclass this to
 * add information to the node.
 * Class hierarchy:
 *     Object
 *       >>>>>>>> GenericNode
 *                     >>>>>>>>>>>>> Any class extending GenericNode
**/

public class GenericNode
{
  private GenericNode next;	// Link to the next node in list

  /*
   * Insert "this" in the linked list after the given node argument.
   */
  public void insertAfter (GenericNode node) {
    this.next = node.next;
    node.next = this;
  }

  /*
   * Insert "this" before the current head.
   */
  public GenericNode insertHead (GenericNode curHead) {
    this.next = curHead;
    return this;
  }

  /*
   * Return the next node after "this" in the linked list.
   */
  public GenericNode getNext () {
    return next;
  }

  /*
   * Apply toString to each node in list headed by "this".
   * Note that if a given node overrides toString() with its own
   * implementation, it will be invoked, rather than the toString()
   * function inherited from Object by GenericNode (polymorphism).
   */
  public void toStringAll () {
    GenericNode node;

    for (node = this; node != null; node = node.getNext())
      System.out.println (node.toString());
  }
}
