Skip to content

Latest commit

 

History

History
63 lines (54 loc) · 1.31 KB

232.md

File metadata and controls

63 lines (54 loc) · 1.31 KB
class MyQueue {
   
   
    private Stack mStackInput = null;
    private Stack mStackOutput = null;

    public MyQueue() {
      mStackInput = new Stack();
      mStackOutput = new Stack();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
      mStackInput.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
      if (mStackOutput.size() == 0) {
        while (mStackInput.size() != 0) {
          int pop = (int) mStackInput.pop();
          mStackOutput.push(pop);
        }
      }
      return (int) mStackOutput.pop();
    }

    /**
     * Get the front element.
     */
    public int peek() {
      if (mStackOutput.size() == 0) {
        while (mStackInput.size() != 0) {
          int pop = (int) mStackInput.pop();
          mStackOutput.push(pop);
        }
      }
      return (int) mStackOutput.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
      return mStackInput.isEmpty() && mStackOutput.isEmpty();
    }
  
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */