package queues; import lrs.*; import java.util.Enumeration; /** * A queue implemented using LRStruct. * * Version 1. * * @author Alan L. Cox * @since 03/15/01 */ public class LRSQueue implements IQueue { private LRStruct _objects = new LRStruct(); /** * Adds an to the LRSQueue. * @param object the object to enqueue * This enqueue is O(n) since it needs to traverse to the tail of the list * to add the element */ public void enqueue(Object object) { _objects.insertEnd(object); } /** * Removes the first object from the LRSQueue. * @return the dequeued object * This dequeue is O(1) since it removes from the front */ public Object dequeue() { return _objects.removeFront(); } /** * Enumerates the queue from oldest to newest object. * @return an Enumeration of the queue. * This enumeration is O(1) since it contains single statements. */ public Enumeration enumeration() { /** * We can't use an anonymous inner class to implement this * Enumeration because of the cosntructor. */ class LRSEnumeration implements Enumeration { private LRStruct _next; LRSEnumeration(LRStruct lrs) { _next = lrs; } public boolean hasMoreElements() { return Boolean.TRUE == _next.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return Boolean.FALSE; } public Object nonEmptyCase(LRStruct host, Object input) { return Boolean.TRUE; } }, null); } public Object nextElement() { return _next.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return null; } public Object nonEmptyCase(LRStruct host, Object input) { Object object = _next.getFirst(); _next = _next.getRest(); return object; } }, null); } } return new LRSEnumeration(_objects); } }