栈实现队列与队列实现栈

目录

栈实现队列

队列实现栈


栈实现队列

leetcode 232

用栈来模拟队列,实现出队,入队,返回队首元素,查看队列是否为空等功能

思路:

用两个栈,一个栈s1用来保存所有入队元素,即每次入队操作的时候直接入栈s1即可,即入队操作只需要用上s1。因为我们只使用栈s1来保存元素,因此判断队列是否为空的时候直接判断该栈是否为空即可。但是如果直接用栈s1出栈的话,其出栈顺序刚好与队列出队顺序相反。

因此我们再用一个栈s2来辅助实现出队和返回队首元素等操作。我们每次出队的时候,都把s1中的元素全部弹出并入栈s2,这样s2中栈顶元素就是s1中的栈底元素,也是我们模拟队列中第一个入队的元素。将其弹出,然后再把s2中的元素全部放回s1即可。因为我们有一个元素,栈s1是用来存放队列数据的,栈s2只是用来辅助弹出以及返回队首元素的。

#include <bits/stdc++.h>
using namespace std;

class MyQueue {
private:
    stack<int> s1; // 模拟入队的栈
    stack<int> s2; // 用于出队的栈
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);

    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        // 第一步,把栈s1中数据转移到s2
        while(!s1.empty()){
            s2.push(s1.top());
            s1.pop();
        }
        // 第二步,取出栈s2中栈顶元素,即为要出队的队首
        int top = s2.top();
        s2.pop();
        // 第三步,把栈s2中的数据放回栈s1
        while(!s2.empty()){
            s1.push(s2.top());
            s2.pop();
        }
        return top;
    }
    
    /** Get the front element. */
    int peek() {
        while(!s1.empty()){
            s2.push(s1.top());
            s1.pop();
        }
        int top = s2.top();
        while(!s2.empty()){
            s1.push(s2.top());
            s2.pop();
        }
        return top;

    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        if(s1.empty()) return true;
        else return false;

    }
};

int main(){
    MyQueue q;
    for(int i = 0; i < 10; ++i){
        q.push(i);
    }
    cout<<q.pop()<<endl;
    cout<<q.peek()<<endl;
    if(q.empty()){
        cout<<"queue empty \n"<<endl;
    }else{
        cout<<"not empty\n"<<endl;
    }
    return 0;
}

队列实现栈

还是用队列来定义一个栈类,实现push,pop,top,empty

基本思路跟栈模拟队列一样,这里用两个队列,一个用来存元素,一个用来做辅助。

在用栈实现队列的时候,为了实现队列的先进先出,我们每次出队的时候都把一个栈中的元素全部出栈放到另一个栈中,这样在一个栈中先入栈进入到栈底的元素在另一个栈中就会是栈顶,因此我们就能顺利用栈模拟出队列先进先出。

栈实现队列是在出队的时候需要辅助,入队的时候不用。

队列实现栈则刚好相反,在入栈的时候需要辅助,我们用队列q1来存放数据,如果要入栈,即来了新数据,我们就将其入到队列q2中去,然后将q1中的元素全部入队q2,这个时候q2中队首元素就是模拟栈中刚入栈的元素,然后q1 q2换一下元素,即实现了栈顶元素就是队列q1的队首元素。每次入栈都按这个过程来做,就能实现栈后进先出的功能。(每次都维持新入栈的元素是队列q1的队首元素)

class MyStack {
private:
    queue<int> q1;
    queue<int> q2;
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        q2.push(x);
        while(!q1.empty()){
            q2.push(q1.front());
            q1.pop();
        }
        while(!q2.empty()){
            q1.push(q2.front());
            q2.pop();

        }

    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int temp = q1.front();
        q1.pop();
        return temp;

    }
    
    /** Get the top element. */
    int top() {
        int r =  q1.front();
        return r;

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

    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值