225. 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 backpeek/pop from frontsize, 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.


题意:

使用队列实现队列的下列操作:

1、push()--将元素x压入栈;

2、pop()--移除栈顶元素;

3、top()--得到栈顶元素;

4、empty()--返回该栈是否是空栈;

Note:

1、只能使用队列的标准操作函数,意思是只能使用push()队列尾,pop()或者peek()队列最前的元素,队列是否是empty()操作是能使用的;

2、根据你的语言,队列可能不支持本地库调用。你可以使用列表或者双向队列模拟一个队列,只要你只使用一个队列的标准操作函数即可;

3、可以假定所有的操作都是有效的(例如,没有pop()或者top()操作将会调用一个空栈);

update:

java中使用MyStack代替Stack功能;


思路一:

该题与Implement Queue using Stacks题目类似,而且刚好相反,目的是想要使用队列的先进先出的特点模拟出堆栈先进后出的特点,主要思路类似于Implement Queue using Stacks题目思路一,使用一个辅助队列实现两次队列顺序翻转,利用队列先进先出的特点实现堆栈先进后出的特点。

代码:C++版:0ms

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        queue<int> tmp; //辅助队列
        while (!oldQueue.empty()) {  //翻转队列存储到辅助队列中
            tmp.push(oldQueue.front());
            oldQueue.pop();
        }
        oldQueue.push(x); //将新增加的元素放入队列中
        while (!tmp.empty()) {  //翻转辅助队列存储到队列中,实现新进后出
            oldQueue.push(tmp.front());
            tmp.pop();
        }
    }


    // Removes the element on top of the stack.
    void pop() {
        oldQueue.pop();
    }


    // Get the top element.
    int top() {
        return oldQueue.front();
    }


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

思路二:

类似于Implement Queue using Stacks题目思路二,采用两个队列实现,一个队列存储新加入队列的值,模拟栈顶操作,另一个队列存储值,以及通过翻转实现栈的后进先出特点。

代码:C++版:2ms

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        q2.push(x);
        while (q2.size() > 1) {  //当q2队列大小大于1时,将q2队列转存到q1队列中
            q1.push(q2.front());
            q2.pop();
        }
    }


    // Removes the element on top of the stack.
    void pop() {
        top();
        q2.pop(); //删除最后加入的元素,即模拟栈顶元素删除
    }


    // Get the top element.
    int top() {
        if (q2.empty()) {
            for (int i = 0; i < (int)q1.size() - 1; ++i) {  //将最后一个元素换到最前面
                q1.push(q1.front());
                q1.pop();
            }
            q2.push(q1.front());  //将最后放入队列的元素放入到q2队列中
            q1.pop();
        }
        return q2.front(); //放回最后放入队列的元素
    }


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值