[原创] 用两个stack实现queue的功能

#include <iostream>
#include <stack>

using namespace std;

class doubleStackToQueue
{
private:
    stack<int> stk_in;
    stack<int> stk_out;
public:
    void push(int element);
    void pop();

};


void doubleStackToQueue::push(int element)
{
    doubleStackToQueue::stk_in.push(element);
}

void doubleStackToQueue::pop()
{
    if(doubleStackToQueue::stk_out.empty()){
        while(!doubleStackToQueue::stk_in.empty()){
            doubleStackToQueue::stk_out.push(doubleStackToQueue::stk_in.top());
            doubleStackToQueue::stk_in.pop();
        }
    }
    if(doubleStackToQueue::stk_out.empty()){
        cout << " no element to pop...." <<endl;
        return;
    }
    else{
        cout << doubleStackToQueue::stk_out.top() << endl;
        doubleStackToQueue::stk_out.pop();
    }
}

int main()
{
    doubleStackToQueue queueDD;
    queueDD.push(1);
    queueDD.push(2);
    queueDD.push(3);
    queueDD.pop();
    return 0;
}
原文地址:https://www.cnblogs.com/lifeinsmile/p/5312925.html