Implement stack using queues

mail@pastecode.io avatar
unknown
c_cpp
10 days ago
890 B
3
Indexable
Never
class MyStack {
private:
        std::queue<int> internalQ;

public:
    MyStack() {
        
    }
    
    void push(int x) {
        std::queue<int> tmpQ;
        tmpQ.push(x);
        while(internalQ.size())
        {
            std::cout<<"Count1 size: "<<internalQ.size()<<" \n";
            tmpQ.push(internalQ.front());
            internalQ.pop();
        }
        internalQ=tmpQ;
    }
    
    int pop() {
        int num=internalQ.front();
        internalQ.pop();
        return num;
    }
    
    int top() {
        return internalQ.front();
    }
    
    bool empty() {
        return internalQ.empty();
    }

};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */
Leave a Comment