/*
  Tom DeDonno from page 691-694 Display 10.9
  
  note private methods, variables are only accessible within the defined class
  This examples make ListNode private and encapuslates it as an inner class
  thereby only methods within this class block have access to ListNode
  
  methods:
  public int length( void )   returns length of link list
  public void addANodeToStart(String ) adds New node at beginning
  public void deleteHeadNode( void )  deletes first node
  public boolean onList(String ) returns true if argument on link list
  private ListNode Find(String ) returns pointer to elment on link list
  public void showList( void ) displays contains of link list
  public String[] arrayCopy( void ) copies contains of link list into array
  
   private class ListNode
    two constructors Null   public ListNode()
    and  public ListNode(String newData, ListNode linkValue)
   
    }
 */

public class StringLinkedListSelfcontained
{
    private ListNode1 head;  //Note want public and private ListNode on Disk

    public StringLinkedListSelfcontained()
    {
        head = null;
    }

    /*****************************************
     *Returns the number of nodes in the list.
     *****************************************/
    public int length()
    {
        int count = 0;
        ListNode1 position = head;
        while (position != null)
        {
            count++;
            position = position.link;
        }
        return count;
    }

    /*****************************************************************
     *Adds a node at the start of the list. The added node has addData
     *as its data. The added node will be the first node in the list.
     *****************************************************************/
    public void addANodeToStart(String addData)
    {
        head = new ListNode1(addData, head);
    }

     public void deleteHeadNode()
    {
        if (head != null)
        {
            head = head.link;
        }
        else
        {
            System.out.println("Deleting from an empty list.");
            System.exit(0);
        }
    }

    public boolean onList(String target)
    {
        return (Find(target) != null);
    }

    /********************************************************************
     *Finds the first node containing the target data, and returns a
     *reference to that node. If key is not in the list, null is returned.
     ********************************************************************/
    private ListNode1 Find(String target)
    {
        ListNode1 position;
        position = head;
        String dataAtPosition;
        while (position != null)
        {
            dataAtPosition = position.data;
            if (dataAtPosition.equals(target))
                return position;
            position = position.link;
        }
        return null;
    }

    public void showList()
    {
        ListNode1 position;
        position = head;
        ListNode1 dataAtPosition;
        while (position != null)
        {
            System.out.println(position.data);
            position = position.link;
        }
    }

    public String[] arrayCopy()
    {
        String[] a = new String[length()];

        ListNode1 position;
        position = head;
        int i = 0;
        while (position != null)
        {
            a[i] = position.data;
            i++;
            position = position.link;
        }

        return a;
    }

    private class ListNode1
    {
        private String data;
        private ListNode1 link;

        public ListNode1()
        {
            link = null;
            data = null;
        }

        public ListNode1(String newData, ListNode1 linkValue)
        {
            data = newData;
            link = linkValue;
        }
    }
}