wayetan
1/2/2014 - 12:53 AM

Implement Queue with 2 Stacks

Implement Queue with 2 Stacks

public class QueuewithTwoStacks<T> {
    private Stack<T> inbox = new Stack<T>();
    private Stack<T> outbox = new Stack<T>();
    public void enqueue(T item){
        inbox.push(item);
    }
    public T dequeue(){
        if(outbox.empty())
            while(!inbox.isEmpty()){
                outbox.push(inbox.pop());
            }
        return outbox.pop();
    }

}