代码随想录算法训练营第十天|理论基础、232.用栈实现队列、225.用队列实现栈

代码随想录算法训练营第十天|理论基础、232.用栈实现队列、225.用队列实现栈

栈与队列理论基础

栈(stack):先进后出

队列(queue):先进先出

https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235346563.png

栈和队列是STL里面的两个数据结构,C++标准库是有多个版本的

  • HP STL
  • P.J.Plauger STL
  • SGI STL:由Silicon Graphics Computer Systems公司参照HP STL实现,被Linux的C++编译器GCC所采用,SGI STL是开源软件,代码可读性高

接下来介绍的都是SGI STL里面的数据结构

栈的出入示意图如下:

https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235434905.png

栈提供push和pop接口,不提供走访功能,也不提供迭代器

栈的底层容器是可插拔的,可以控制使用哪种容器来实现栈的功能

STL中栈往往不被归类为容器,而被归类为container adapter(容器适配器)

栈的底层实现可以是vector、deque、list,主要是数组和链表的底层实现

https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235459376.png

常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构

deque是双向队列,封住一端,开通另一端即可实现栈

也可以指定vector为栈的底层实现,初始化如下

std::stack<int, std::vector<int>> third; // 使用vector为底层容器的栈

队列的情况类似

SGI STL中队列一样以deque为缺省情况下的底部结构

也可以指定list为底层实现,初始化queue的语句如下:

std::queue<int, std::list<int>> third; // 使用list为底层容器的队列

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

示例 1:

输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

思路

这道题目要完成队列的基本接口

  • push
  • pop
  • peek
  • empty

可以使用栈的基本接口来完成

具体思路如下:

https://code-thinking.cdn.bcebos.com/gifs/232.用栈实现队列版本2.gif

模拟队列的入队时,将数据放入输入栈即可

模拟队列的出队时,要判读输出栈是否为空,如果空的话,将进栈的数据全部导入到输出栈,在弹出数据,如果不为空,可以直接出站

如果队列为空,则需要判断输入栈和输出栈同时为空即可

还有peek可以服用出队的逻辑,先将数据pop掉再添加回去

具体代码如下:

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 result = stOut.top();
        stOut.pop();
        return result;
    }
    
    int peek() {
        int res = this->pop();
        stOut.push(res);
        return res;
    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

/**
 * 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. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false

示例:

输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]

解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

思路

本题用两个队列que1和que2实现栈的功能,具体思路如下:

https://code-thinking.cdn.bcebos.com/gifs/225.用队列实现栈.gif

这里que2起备份的作用

具体代码如下:

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

    }
    
    void push(int x) {
        que1.push(x);
    }
    
    int pop() {
        int size = que1.size();
        size--;
        while(size--){
            que2.push(que1.front());
            que1.pop();
        }

        int result = que1.front();
        que1.pop();
        que1 = que2;
        while(!que2.empty()){
            que2.pop();
        }
        return result;
    }
    
    int top() {
        return que1.back();
    }
    
    bool empty() {
        return que1.empty();
    }
};

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

📎 参考文章

代码随想录栈与队列理论基础

代码随想录0232.用栈实现队列

代码随想录0225.用队列实现栈

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值