Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6
package rac; import lrs.*; /* * Implements a factory for restricted access containers, including a * container that returns a random item. */ public class RandomRACFactory extends ALRSRACFactory { /** * Create a container that returns a random item. */ public IRAContainer makeRAC() { return new LRSRAContainer(new IAlgo() { public Object emptyCase(LRStruct host, Object... input) { return host.insertFront(input[0]); } public Object nonEmptyCase(LRStruct host, Object input) { /* * Math.Random returns a value between 0.0 and 1.0. */ if (0.5 > Math.random()) return host.insertFront(input[0]); else return host.getRest().execute(this, input); } }); } }
But can we push the abstraction further? Is the difference between a stack and a queue really anything more than how the data is ordered? Now, let's go on an look at the ordering object and priority queues...
- 1489 reads