
public class LinkedList2
{
    private class Node2
    {
        private String item;
        private int count;
        private Node2 link;

        public Node2( )
        {
             item = null;
             count = 0;
             link = null;
        }

        public Node2(String newItem, int newCount, Node2 linkValue)
        {
            item = newItem;
            count = newCount;
            link = linkValue;
        }
    }//End of Node2 inner class

    private Node2 head;

    public LinkedList2( )
    {
        head = null;
    }

    /**
     Adds a node at the start of the list with the specified data.
     The added node will be the first node in the list.
    */
    public void add(String itemName, int itemCount)
    {
        head = new Node2(itemName, itemCount, head);
    }

    /**
     Removes the head node and returns true if the list contains at least
     one node. Returns false if the list is empty.
    */
    public boolean deleteHeadNode( )
    {
        if (head != null)
        {
            head = head.link;
            return true;
        }
        else
            return false;
    }

    /**
     Returns the number of nodes in the list.
    */
    public int size( )
    {
        int count = 0;
        Node2 position = head;
        while (position != null)
        {
            count++;
            position = position.link;
        }
        return count;
    }

    public boolean contains(String item)
    {
        return (find(item) != null);
    }

    /**
     Finds the first node containing the target item, and returns a
     reference to that node. If target is not in the list, null is returned.
    */
    private Node2 find(String target)
    {
        Node2 position = head;
        String itemAtPosition;
        while (position != null)
        {
            itemAtPosition = position.item;
            if (itemAtPosition.equals(target))
                return position;
            position = position.link;
        }
        return null; //target was not found
    }

    public void outputList( )
    {
        Node2 position = head;
        while (position != null)
        {
            System.out.println(position.item + " "
                                       + position.count);
            position = position.link;
        }
    }

    public boolean isEmpty( )
    {
        return (head == null);
    }

    public void clear( )
    {
        head = null;
    }
}



