用兩個棧實現一個隊列

題目要求如下
`http://www.lintcode.com/zh-cn/problem/implement-queue-by-two-stacks/
class Queue {
public:
stack stack1;
stack stack2;

Queue() {

}

void push(int element) {
    stack1.push(element);
}

int pop() {
    adjust();
    int temp = stack2.top();
    stack2.pop();
    return temp;
}

void adjust()
{
    if (stack2.empty())
    {
        while(!stack1.empty())
        {
            stack2.push(stack1.top());
            stack1.pop();
        }
    }
}
int top() {
    adjust();
    return stack2.top();
}

};

`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章