代码随想录第十天 LeetCode 232、225(栈,队列)

栈、队列简介

  • 栈(stack):栈是STL中的一种数据结构,数据先进后出栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能),所以在STL中栈往往不被归类为容器,而被归类为container adapter(容器适配器)
    下面介绍一下常用函数:

    stack<int> st;
    st.push(); //压入
    st.pop(); //弹出
    st.top(); //返回栈顶元素
    st.size();
    st.empty();
    
  • 队列(queue):队列也是STL中的一种数据结构,数据先进先出。和栈一样,队列在STL中也是一种容器适配器
    常用函数:

    queue<int> que;
    que.push(); //队尾插入
    que.pop(); //队首弹出
    que.front(); //返回队首
    que.back(); //返回队尾
    que.empty();
    que.size();
    

两者在STL中底层都是由deque即双向队列实现,其中栈可以使用list、deque和vector实现,而队列不能使用vector实现,因为queue转换器要求容器支持front()、back()操作。

232.用栈实现队列

本题使用两个栈来模拟队列,一个做输入,一个做输出。唯一需要注意的就是模拟pop弹出的操作,经过模拟可以发现,输出栈如果为空,就把进栈数据全部导入进来,再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。下面直接给出代码

class MyQueue {
public:
    stack<int> stin;
    stack<int> stout;
    MyQueue() {

    }
    
    void push(int x) {
        stin.push(x);
    }
    
    int pop() {
        if(!stout.empty()){
            int temp = stout.top();
            stout.pop();
            return temp;
        }
        else{
            int temp;
            while(!stin.empty()){
                temp = stin.top();
                stout.push(temp);
                stin.pop();
            }
            stout.pop();
            return temp;
        }
    }
    
    int peek() {
        if(!stout.empty()) return stout.top();
        else{
            int temp;
            while(!stin.empty()){
                temp = stin.top();
                stout.push(temp);
                stin.pop();
            }
            return temp;
        }
    }
    
    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();
 */

这里在写peek()函数时,可以直接使用this指针调用pop方法:

	int peek() {
        int res = this->pop(); // 直接使用已有的pop函数
        stout.push(res); // 因为pop函数弹出了元素res,所以再添加回去
        return res;
    }

225. 用队列实现栈

其实也是一个模拟的过程,并不难,直接看代码:

class MyStack {
public:
    queue<int> que;
    MyStack() {

    }
    
    void push(int x) {
        que.push(x);
    }
    
    int pop() {
        for(int i = 1; i < que.size(); i++){
            int temp = que.front();
            que.pop();
            que.push(temp);
        }
        int temp = que.front();
        que.pop();
        return temp;
    }
    
    int top() {
        int temp = this->pop();
        que.push(temp);
        return temp;
    }
    
    bool empty() {
        if(que.empty()) return true;
        else 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();
 */

继续加油!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值