Leetcode 225 两个队列实现栈

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

  • You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

由于是队列,后进后出,栈顶元素在队列的尾部,结合一个flag判断栈顶在哪个队列。一开始两个队列都为空的时候,随便插入其中一个,比如q1,将q1的标志设为1,q2这时为空。如果要出栈,则将q1最后元素之前的所有元素出列,加入q2,并将标志设为q2,q1最后一个元素出列,此时q1为空.当要插入时,选择两者中为空的那个队列插入,并设标志为新插入的那个队列。

实现写了70+行,还需要优化

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        if (flag) //栈顶在队列1,继续加入
            q1.push(x);
        else
            q2.push(x);
    }

    // Removes the element on top of the stack.
    void pop() {
        if (!empty()) {
            if (flag) {//栈顶在队列1,将队列1转移到队列2
                while (!q1.empty()) {
                    int temp = q1.front();
                    q1.pop();
                    if (q1.empty()) //弹出的是最后一个元素
                        flag = false; //栈顶在队列2
                    else //尚不是最后一个,则加入队列2
                        q2.push(temp);
                }
            }
            else { //栈顶在队列2
                while (!q2.empty()) {
                    int temp = q2.front();
                    q2.pop();
                    if (q2.empty()) //弹出的是最后一个元素
                        flag = true; //栈顶在队列1
                    else //尚不是最后一个,则加入队列1
                        q1.push(temp);
                }    
            }            
        }
    }

    // Get the top element.
    int top() {
        int res = 0;
        if (flag) {//栈顶在队列1,将队列1转移到队列2
            while (1) {
                res = q1.front();
                q1.pop();
                if (q1.empty()) {//弹出的是最后一个元素
                    q1.push(res);//再压回去
                    break;
                }
                else //尚不是最后一个,则加入队列2
                    q2.push(res);
            }
        }
        else { //栈顶在队列2
            while (1) {
                res = q2.front();
                q2.pop();
                if (q2.empty()) {//弹出的是最后一个元素
                    q2.push(res);
                    break; 
                }
                else //尚不是最后一个,则加入队列1
                    q1.push(res);
            }
        }        
        return res;
    }

    // Return whether the stack is empty.
    bool empty() {
        return q1.empty()&&q2.empty();
    }
    queue<int> q1,q2;
    bool flag = true; //true表示栈顶在队列1,false表示栈顶在队列2
};

 其他博客的实现

http://blog.csdn.net/xy010902100449/article/details/49307823

http://blog.csdn.net/sunao2002002/article/details/46482673

原文地址:https://www.cnblogs.com/wacc/p/4905399.html