算法训练营第十天|232.用栈实现队列 225. 用队列实现栈

文章讲述了如何用栈和队列在LeetCode中的两个问题(232.用栈实现队列和225.用队列实现栈)中进行数据结构的转换,以及通过这两个问题深化对C++STL特别是队列适配器和this指针的理解。
摘要由CSDN通过智能技术生成

LeetCode232.用栈实现队列

文章链接:代码随想录
题目链接:232.用栈实现队列

思路:熟悉栈和队列的基本操作,明白互相实现的原理就可以,很简单

class MyQueue {
public:
    MyQueue() {

    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if (stOut.empty()){
            while (!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int res = stOut.top();
        stOut.pop();
        return res;
    }
    
    int peek() {
        int res = this -> pop();
        stOut.push(res);
        return res;
    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }

private:
    stack<int> stIn;
    stack<int> stOut;
};

/**
 * 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();
 */

LeetCode225. 用队列实现栈

文章链接:代码随想录
题目链接:225. 用队列实现栈

思路:用一个队列即可实现,两个队列另一个也是备用,反而操作麻烦一些。
两个队列实现:

class MyStack {
public:
    MyStack() {

    }
    
    void push(int x) {
        que.push(x);
    }
    
    int pop() {
        int size = que.size() - 1;
        while (size--){
            que1.push(que.front());
            que.pop();
        }
        int res = que.front();
        que.pop();
        while (!que1.empty()){
            que.push(que1.front());
                que1.pop();
        }
        return res;
    }
    
    int top() {
        int res = this -> pop();
        que.push(res);
        return res;
    }
    
    bool empty() {
        return que.empty();
    }

private:
    queue<int> que;
    queue<int> que1;
};

/**
 * 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();
 */

一个队列实现:

class MyStack {
public:
    MyStack() {

    }
    
    void push(int x) {
        que.push(x);
    }
    
    int pop() {
        int size = que.size() - 1;
        while (size--){
            que.push(que.front());
            que.pop();
        }
        int res = que.front();
        que.pop();
        return res;
    }
    
    int top() {
        int res = this -> pop();
        que.push(res);
        return res;
    }
    
    bool empty() {
        return que.empty();
    }

private:
    queue<int> que;
};

/**
 * 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();
 */

今天题目很简单,主要是对stack和queue容器适配器的概念加深了理解,另外这段刚好在听侯捷的STL的课,对C++标准库的版本和编译器的了解更全面了:我们现在所使用的是SGI版本的STL,适配Linux下的g++编译器。
另外今天做题遇到了this指针的操作,看了几篇文章,大致就是this可以在成员函数内部调用,指向这个对象本身,可以调用这个对象的数据或函数。this涉及的知识和操作很多,以后再深入学习。
第十天打卡,这几天配合着刷题和看侯捷老师STL的课,感觉对C++和STL的理解清晰了很多,加油!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值