【C++】用队列实现栈 / 用栈实现队列 / 实现带最小值的栈

C++的 STL stack和 STL queue

头文件分别是 <stack> 和 <queue>
注意:使用需要namespace std

  • STL stack基本操作
stack<int> S;
S.empty();
S.push(10);
S.pop();
  • STL queue基本操作
queue<int> Q;
Q.empty();
Q.push(5);
Q.pop();
Q.front();

用队列数据结构实现栈

使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空

// 用队列数据结构实现的stack类
class MyStack {
    
    
public:
    /** Initialize your data structure here. */
    MyStack() {
    
    
    }

    /** Push element x onto stack. */
    void push(int x) {
    
    
        queue<int> temp_queue;
        temp_queue.push(x);
        while (!_data.empty()) {
    
    
            // 将数据队列元素在导入临时队列
            temp_queue.push(_data.front());
            _data.pop();
        }
        while (!temp_queue.empty())
        {
    
    
            // 将临时队列元素导入数据队列做保存
            _data.push(temp_queue.front());
            temp_queue.pop();
        }
    }

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

    /** Get the top element. */
    int top() {
    
    
        return _data.front();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
    
    
        return _data.empty();
    }
private:
    queue<int> _data;
};

用栈数据结构实现队列

使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。

思路:
在这里插入图片描述

class MyQueue {
    
    
public:
    /** Initialize your data structure here. */
    MyQueue() {
    
    
    }

    /** Push element x to the back of queue. */
    void push(int x) {
    
    
        stack<int> temp_stack;
        while (!_data.empty()) {
    
    
            // 将数据栈中的数据导入到临时栈
            temp_stack.push(_data.top());
            _data.pop();
        }
        // 把要加入的数据加入栈
        temp_stack.push(x);
        while (!temp_stack.empty())
        {
    
    
            // 将临时栈中的数据导入到数据栈保存
            _data.push(temp_stack.top());
            temp_stack.pop();
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    
    
        int x = _data.top();
        _data.pop();
        return x;
    }

    /** Get the front element. */
    int peek() {
    
    
        return _data.top();
    }

    /** Returns whether the queue is empty. */
    bool empty() {
    
    
        return _data.empty();
    }

private:
    std::stack<int> _data;
};

实现带有最小值的栈结构

设计一个能在常数时间内检索到最小元素的栈。
由于题目要求要在常数时间复杂度实现,故不用遍历方式找出最小值。根据栈的储存和pop特点,我们并不能用一个变量取储存当前栈的最小值或最大值,你想想,你现在储存的当前栈的最小值刚好是栈顶的元素,下一步对栈做pop弹出操作后,栈的最小值或最大值就发生改变了,故最小值变量并不能实现,所以要用一个最小值栈来储存当前栈的最小值。

class MinStack {
    
    
public:
    /** initialize your data structure here. */
    MinStack() {
    
    
    }

    void push(int x) {
    
    
        // 先把数据入数据栈
        _data.push(x);
        // 根据数据和最小值栈的栈顶关系来取决要入最小值栈的值
        if (!_min.empty() && x > _min.top()) {
    
    
            x = _min.top();
        }
        _min.push(x);
    }

    void pop() {
    
    
        _data.pop();
        _min.pop(); // 出栈也要同步最小值栈!
    }

    int top() {
    
    
        return _data.top();
    }

    int getMin() {
    
    
        return _min.top();
    }
private:
    std::stack<int> _data;
    std::stack<int> _min;
};

ps: 小象学院教程 https://www.bilibili.com/video/BV1GW411Q77S?t=7029&p=2 的笔记
LeetCode题号: 225,232, 155

猜你喜欢

转载自blog.csdn.net/weixin_44427114/article/details/107888720