day10_232栈实现队列_225队列实现栈

232 用栈实现队列

  1. 对于栈的操作
  • stIn.top():返回栈顶。
  • stIn.pop():弹出栈顶。
  • stIn.empty():返回栈是否为空,true为空。
  • stIn.push():向栈中插入元素。
  1. 对于队列的操作
  • push(x) : 将一个元素放入队列的尾部。
  • pop() : 从队列首部移除元素。
  • peek() : 返回队列首部的元素。
  • empty() : 返回队列是否为空。
  1. 两个栈实现队列
  • 一个负责入栈stIn,后进先出。
  • 一个负责接受栈stIn出栈的数据,先进后出。
  • 通过栈后进先出,先进后出实现队列的先进先出。
class MyQueue {
public:
    stack<int> stIn;//第一个入栈
    stack<int> stOut;//第二个入栈
    MyQueue() {//默认构造函数
    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {//从队列开头移除并返回元素
        if(stOut.empty()){
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int tmp = stOut.top();
        stOut.pop();
        return tmp;
    }
    
    int peek() {
         if(stOut.empty()){
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int tmp = stOut.top();
        return tmp;
    }
    
    bool empty() {
        if(stIn.empty() && stOut.empty()) return true;
        return false;
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

225 用队列实现栈

  1. 两个队列:
  • 一个队列实现保存元素;
  • 一个队列始终为空,接受另一个队列的n-1个元素,如果是pop,返回另一个队列的最后一个元素并删除,如果是top,返回另一个队列的最后一个元素并插入到这个队列中。用队列的先进先出实现栈的先进后出。
class MyStack {
public:
   queue<int> qIn1;//负责入队和接受qIn2中的n-1个队中元素
   queue<int> qIn2;//负责入队和接受qIn1中的n-1个队中元素
   MyStack() {

   }
   
   void push(int x) {
       qIn1.push(x);
   }
   
   int pop() {
       if(qIn1.empty() && !qIn2.empty()){
           int tmp = qIn2.front();
           qIn2.pop();
           while(!qIn2.empty()){
               qIn1.push(tmp);
               tmp = qIn2.front();
               qIn2.pop();
           }
           return tmp; 
       }
       else {
           int tmp = qIn1.front();
           qIn1.pop();
           while(!qIn1.empty()){
               qIn2.push(tmp);
               tmp = qIn1.front();
               qIn1.pop();
           }
           return tmp;  
       }
   }
   
   int top() {
       if(qIn1.empty() && !qIn2.empty()){
           int tmp = 0;
           while(!qIn2.empty()){
               tmp = qIn2.front();
               qIn2.pop();
               qIn1.push(tmp);
           }
           return tmp; 
       }
       else {
           int tmp = 0;
           while(!qIn1.empty()){
               tmp = qIn1.front();
               qIn1.pop();
               qIn2.push(tmp);
           }
           return tmp; 
       }
   }
   
   bool empty() {
       if(qIn1.empty() && qIn2.empty()) return true;
       return false;
   }
};

/**
* 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();
*/
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值