package stacks; import lrs.*; import java.util.Enumeration; /** * A stack implemented using LRStruct. * * A stack manages its objects in a Last In, First Out (LIFO) order. * * @author Alan L. Cox * @since 03/15/01 */ public class LRSStack implements IStack { private LRStruct _objects = new LRStruct(); /** * Adds a new object to the LRSStack. * @param data is new object added to the stack. * This push method is O(1) since it only adds to the front of the list. */ public void push(Object data) { _objects.insertFront(data); } /** * Removes and returns the newest object from the stack. * *@return the popped object * This pop method is O(1) since it only removes from the front of the list */ public Object pop() { return _objects.removeFront(); } /** * Enumerates the stack from newest to oldest object. * @return an Enumeration of the stack. * This enumeration method 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); } }