JavaAlgorithms
Elementary and no so elementary Java algorithms
listAlgorithms/TwoStackQueue.java
Go to the documentation of this file.
00001 
00009 package listAlgorithms;
00010 
00034 public class TwoStackQueue<T extends Comparable<T>> {
00035 
00036     private Stack<T> mStore = new Stack<T>();
00037     private Stack<T> mQueue = new Stack<T>();
00038 
00039     protected void copyToStore() {
00040         while (mQueue.peek() != null) {
00041             mStore.push(mQueue.pop());
00042         }
00043     }
00044 
00045     protected void copyFromStore() {
00046         while (mStore.peek() != null) {
00047             mQueue.push(mStore.pop());
00048         }
00049     }
00050 
00056     public void enqueue(T val) {
00057         copyToStore();
00058         mQueue.push(val);
00059         copyFromStore();
00060     }
00061 
00062     public T peek() {
00063         T queueVal = mQueue.peek();
00064         return queueVal;
00065     }
00066 
00072     public T dequeue() {
00073         T queueVal = mQueue.pop();
00074         return queueVal;
00075     }
00076 }
 All Classes Namespaces Files Functions Variables