用两个栈实现一个队列

来源:互联网 发布:淘宝联盟 流量精灵 编辑:程序博客网 时间:2024/05/04 13:51

题目要求如下
`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();}

};

`

0 0
原创粉丝点击