C# inplementation.


  • 3
    D
    public class Queue {
    // Push element x to the back of queue.
    Stack input = new Stack();
    Stack output = new Stack();
    public void Push(int x) {
        input.Push(x);
    }
    
    // Removes the element from front of queue.
    public void Pop() {
        Peek();
        output.Pop();
    }
    
    // Get the front element.
    public int Peek() {
        if(output.Count==0){
            while(input.Count!=0){
                output.Push(input.Pop());
            }
        }
        return Convert.ToInt32(output.Peek());
    }
    
    // Return whether the queue is empty.
    public bool Empty() {
        if(input.Count==0&&output.Count==0)return true;
        else return false;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.