用队列实现栈

题目:用队列实现栈

题目链接
我的思路:
初始化两个队列queue
push操作:数据直接送入队列1中。
pop操作:将队列1中的元素依次送入到队列2暂存直到队列1中只剩下一个元素,然后将该元素返回并pop掉,之后再将队列2中的元素送回queue中。
top操作:与pop操作类似,将队列1中的元素依次送入到队列2暂存直到队列1中只剩下一个元素,然后将该元素记录下并返回,之后送入队列2中,之后再将队列2中的元素依次送入到队列1中。

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
            q1.push(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int x;
        if(q1.size()==1){
            x = q1.front();
            q1.pop();
            return x;
            }
        while(!q1.empty()){
            q2.push(q1.front());
            q1.pop();
            if(q1.size()==1){
                x = q1.front();
                q1.pop();
            }
            
        }
        while(!q2.empty()){
            q1.push(q2.front());
            q2.pop();
        }
        return x;
    }
    
    /** Get the top element. */
    int top() {
        int x;
        while(!q1.empty()){
            if(q1.size()==1)
                x = q1.front();
            q2.push(q1.front());
            q1.pop();
        }
        while(!q2.empty()){
            q1.push(q2.front());
            q2.pop();
        }
        return x;
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        if(!q1.empty())
            return false;
        else 
            return true;

    }
    queue<int> q1;
    queue<int> q2;
};
/*解题思路:使用两个队列,以q1为主,栈操作遵循后进先出的原则,pop操作就是先将队列的元素挨个POP到q2队列直到q1队列的只剩一个元素,将该元素直接POP掉,之后再将q2队列的元素重新POP回q1队列;*/
/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

代码随想录中直接先计算出队列的size,然后pop到留一个元素,且top操作直接使用back函数。

class MyStack {
public:
   queue<int> que1;
   queue<int> que2; // 辅助队列,用来备份
   /** Initialize your data structure here. */
   MyStack() {

   }

   /** Push element x onto stack. */
   void push(int x) {
       que1.push(x);
   }

   /** Removes the element on top of the stack and returns that element. */
   int pop() {
       int size = que1.size();
       size--;
       while (size--) { // 将que1 导入que2,但要留下最后一个元素
           que2.push(que1.front());
           que1.pop();
       }

       int result = que1.front(); // 留下的最后一个元素就是要返回的值
       que1.pop();
       que1 = que2;            // 再将que2赋值给que1
       while (!que2.empty()) { // 清空que2
           que2.pop();
       }
       return result;
   }

   /** Get the top element. */
   int top() {
       return que1.back();
   }

   /** Returns whether the stack is empty. */
   bool empty() {
       return que1.empty();
   }
}; 

另外,可以只使用一个队列完成,也就是原来要pop到队列2的都pop到队列本身的末尾。
写到这里,就很想知道STL中是如何为queue分配内存的!后续将会补上链接…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值