
import java.util.*;

public class LinkedList<E>
{
    private ListNode head;

    public LinkedList( )
    {
        head = null;
    }

    public int length( )
    {
        int count = 0;
        ListNode position = head;
        while (position != null)
        {
            count++;
            position = position.link;
        }
        return count;
    }

    /**
     The added node will be the first node in the list.
    */
    public void addANodeToStart(E addData)
    {
        head = new ListNode(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(E target)
    {
        return (Find(target) != null);
    }

    /**
     Finds the first node containing the target data,
     and returns a reference to that node. If target is not
     in the list, null is returned.
    */
    private ListNode Find(E target)
    {
        ListNode position;
        position = head;
        E dataAtPosition;
        while (position != null)
        {
            dataAtPosition = position.data;
            if (dataAtPosition.equals(target))
                return position;
            position = position.link;
        }
        //target was not found.
        return null;
    }

    public void showList( )
    {
        ListNode position;
        position = head;
        ListNode dataAtPosition;
        while (position != null)
        {
            System.out.println(position.data);
            position = position.link;
        }
    }


    public Vector<E> vectorCopy( )
    {
        Vector<E> v = new Vector<E>(length( ));

        ListNode position;
        position = head;

        while (position != null)
        {
            v.addElement(position.data);
            position = position.link;
        }

        return v;
    }



    private class ListNode
    {
        private E data;
        private ListNode link;

        public ListNode( )
        {
            link = null;
            data = null;
        }

        public ListNode(E newData, ListNode linkValue)
        {
            data = newData;
            link = linkValue;
        }
    }

}



