1
  2
/**
  3  * A Quack of objects
  4  *
  5  * This implementation uses linked memory
  6  *
  7  * @version 1.0
  8  * @author  Prof. David Bernstein, James Madison University
  9  */
 10     public class Quack
 11    {
 12       private Node first, last;
 13    
 14    
 15     /**
 16      * Construct a new (empty) Quack
 17      */
 18        public Quack()
 19       {
 20          first = null;
 21          last  = null;
 22       }
 23    
 24    
 25    
 26    
 27     /**
 28      * Pep an Object off of this Quack
 29      *
 30      * @return  The Object
 31      */
 32        public Object pep()
 33       {
 34          Node    temp;
 35          Object  value;
 36          value = null;
 37       
 38          if (first != null) // if first is pointing somewhere
 39          {
 40             value = first.value;
 41             if (first == last)   // asks whether first is pointing where last is pointing
 42             {
 43                first = null;    // make first point to null (point nowhere)
 44             }
 45             else
 46             {
 47                temp  = last;  // makes temp point where last is pointing
 48                while (temp.next != first)
 49                {
 50                   temp = temp.next; // makes temp point where temp.next is pointing
 51                }
 52                first = temp;  // makes first point where temp is pointing
 53                first.next = null;  // makes first.next point to null
 54             }
 55          }
 56       
 57          if (first == null) // if first is pointing to null ( point nowhere)

 58             last = null;   // make last point to null (point nowhere)
 59          return value;
 60       }
 61    
 62    
 63    
 64    
 65    
 66    
 67     /**
 68      * Pip an Object off of this Quack
 69      *
 70      * @return  The Object
 71      */
 72        public Object pip()
 73       {
 74          Object  value;
 75       
 76       
 77          if (last != null)
 78          {
 79             value = last.value;
 80             last = last.next;
 81          }
 82          else
 83          {
 84             value = null;
 85          }
 86          if (last == null) first = null;
 87       
 88          return value;
 89       }
 90    
 91    
 92    
 93    
 94    
 95    
 96    
 97    
 98     /**
 99      * Push an Object onto this Quack
100      *
101      * @param anObject   The Object to push
102      */
103        public void push(Object anObject)
104       {
105          Node temp;
106       
107          temp = new Node();
108       
109          temp.value = anObject;
110          temp.next = last;
111       
112          last = temp;
113          if (first == null) first = last;
114       }
115    }
116