<LeetCode OJ> 225. Implement Stack using Queues

225. Implement Stack using Queues

Total Accepted: 26676  Total Submissions: 88051  Difficulty: Easy

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和2,举例,比如1,2,3,4依次压进队列1,则1应该是栈首,4是栈底
1,进栈:全部压进队列1
2,取栈顶元素:将队列1的元素转给队列2,队列1只剩1个即可,此元素就是栈首,最后再次压入队列2,使队列1为空,也就是说始终保持队列1或者2为空
3,弹出栈顶元素:如同取栈顶元素一样,留下的那一个弹出
4,是否为空?显然来个队列为空就是空栈。

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        if(!q1.empty())//q1不为空
           q1.push(x); 
        else
           q2.push(x); 
        newtop=x;//新进来的元素就是栈顶元素,pop时一定要更新这个值
    }

    // Removes the element on top of the stack.
    void pop() {
        if(!q1.empty())//q1不为空
         {  
             while(q1.size()>1)
             {
                 int val=q1.front();
                 if(q1.size() == 2)
                    newtop = val;
                 q2.push(val);
                 q1.pop();
             }
             q1.pop();
         }
        else
          { 
              while(q2.size()>1)
             {
                 int val=q2.front();
                 if(q2.size() == 2)
                    newtop = val;
                 q1.push(val);
                 q2.pop();
             }
             q2.pop(); 
          }
    }

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

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


来自别人家的思路:

只申请一个队列,每次数据压进来的时候,直接就将数据调整为栈的顺序
将x压入队列,接着调整:取队首元素(先进来的元素)重新压倒队尾,操作que.size()-1次
牛逼.......

class Stack {
public:
    queue<int> que;
    // Push element x onto stack.
    void push(int x) {
        que.push(x);
        for(int i=0;i<que.size()-1;++i){
            que.push(que.front());
            que.pop();
        }
    }

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

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

    // Return whether the stack is empty.
    bool empty() {
        return que.empty();
    }
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50444995

原作者博客:http://blog.csdn.net/ebowtang

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值