zoukankan      html  css  js  c++  java
  • Leetcode题目:Implement Stack using Queues

    题目: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).

    题目解答:题目中要求使用队列来实现栈的功能。其中,对于队列只可以使用它的push,pop,size,empty操作。

    代码为:

    class Stack {
    public:
        // Push element x onto stack.
        void push(int x) {
            if(q1.empty())
            {
                q2.push(x);
            }
            else
            {
                q1.push(x);
            }
        }

        // Removes the element on top of the stack.
        void pop() {
            if(q1.empty())
            {
                while(q2.size() > 1)
                {
                    q1.push(q2.front());
                    q2.pop();
                }
                if(q2.size() == 1)
                    q2.pop();
            }
            else
            {
                while(q1.size() > 1)
                {
                    q2.push(q1.front());
                    q1.pop();
                }
                if(q1.size() == 1)
                    q1.pop();
            }
        }

        // Get the top element.
        int top() {
            int tmp = -1;
            if(q1.empty())
            {
                while(q2.size() > 1)
                {
                    q1.push(q2.front());
                    q2.pop();
                }
                if(q2.size() == 1)
                {
                    tmp = q2.front();
                    q1.push(q2.front());
                    q2.pop();
                }
            }
            else
            {
                while(q1.size() > 1)
                {
                    q2.push(q1.front());
                    q1.pop();
                }
                if(q1.size() == 1)
                {
                    tmp = q1.front();
                    q2.push(q1.front());
                    q1.pop();
                }
            }
            return tmp;
        }

        // Return whether the stack is empty.
        bool empty() {
            return q1.empty() && q2.empty();
        }
    private:
        queue<int> q1;
        queue<int> q2;
    };

  • 相关阅读:
    《构建之法》阅读有疑 与 个人Week1作业
    版本管理和项目管理软件浅谈
    [2019BUAA软工助教]第0次个人作业
    [2017BUAA软工助教]博客格式的详细说明
    [2017BUAA软工助教]收集个人信息
    最长英文单词串题目分析
    蓝桥杯PREV-11:横向打印二叉树
    day2
    冯如杯day1
    个人阅读作业
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5431412.html
Copyright © 2011-2022 走看看