Untitled

 avatar
user_9124840
plain_text
24 days ago
793 B
2
Indexable
Never
class MyQueue {
  stack<int>inStack;
  stack<int>outStack;
public:
    MyQueue() {
        
    }
    
    void push(int x) {
        inStack.push(x);
    }
    
    int pop() {
        if(outStack.empty()){

            while(!inStack.empty()){
                outStack.push(inStack.top());
                inStack.pop();
            }       
        }
        int x=outStack.top();
        outStack.pop();
        return x;
    }
    
    int peek() {
          if(outStack.empty()){

            while(!inStack.empty()){
                outStack.push(inStack.top());
                inStack.pop();
            }       
        }
        return outStack.top();
    }
    
    bool empty() {
      return (inStack.empty()&& outStack.empty());
    }
};
Leave a Comment