/**
 * A Quack of objects
 *
 * This implementation uses linked memory
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class Quack
{
    private Node first, last;


    /**
     * Construct a new (empty) Quack
     */
    public Quack()
    {
	first = null;
	last  = null;
    }




    /**
     * Pep an Object off of this Quack
     *
     * @return  The Object
     */
    public Object pep()
    {
	Node    temp;
	Object  value;


	value = null;

	if (first != null) 
        {
	    value = first.value;

	    if (first == last)
	    {
		first = null;
	    }
	    else
	    {
		temp  = last;
		while (temp.next != first)
		{
			temp = temp.next;
		}
		
		first = temp;
		first.next = null;
	    }
	} 

	if (first == null) last = null;

	return value;
    }






    /**
     * Pip an Object off of this Quack
     *
     * @return  The Object
     */
    public Object pip()
    {
	Object  value;


	if (last != null) 
        {
	    value = last.value;
	    last = last.next;
	} 
	else
	{
	    value = null;
	}
	if (last == null) first = null;

	return value;
    }








    /**
     * Push an Object onto this Quack
     *
     * @param anObject   The Object to push
     */
    public void push(Object anObject)
    {
	Node temp;

	temp = new Node();

	temp.value = anObject;
	temp.next = last;

	last = temp;
	if (first == null) first = last;
    }
}
