侧边栏壁纸
博主头像
thinkTV博主等级

喜爱动漫的二刺螈一枚,摩托车云爱好者(快要有车了)。 懂一点技术的在读生物医学工程研究生( •̀ ω •́ )✧,多多指教。

  • 累计撰写 127 篇文章
  • 累计创建 17 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

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

thinkTV
2023-04-28 / 0 评论 / 0 点赞 / 789 阅读 / 1,665 字 / 正在检测是否收录...

1. 栈与队列理论基础

代码随想录: 原文

1.1 三个最为普遍的STL版本

  1. HP STL 其他版本的C++ STL,一般是以HP STL为蓝本实现出来的,HP STL是C++ STL的第一个实现版本,而且开放源代码。
  2. P.J.Plauger STL 由P.J.Plauger参照HP STL实现出来的,被Visual C++编译器所采用,不是开源的。
  3. SGI STL 由Silicon Graphics Computer Systems公司参照HP STL实现,被Linux的C++编译器GCC所采用,SGI STL是开源软件,源码可读性甚高。

接下来介绍的栈和队列也是SGI STL里面的数据结构

1.2 栈

栈先进后出

图片-1682649997815

  • 栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则
  • 栈不提供走访功能,也不提供迭代器(iterator)
  • 栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)
  • STL中栈往往不被归类为容器,而被归类为container adapter(容器适配器)
  • 栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现

图片-1682650089324

  • 常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构
  • deque是一个双向队列,只要封住一段,只开通另一端就可以实现栈的逻辑了

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

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

1.3 队列

  • 队列的情况与栈的特性是一样的
  • 队列中先进先出的数据结构,同样不允许有遍历行为,不提供迭代器, SGI STL中队列一样是以deque为缺省情况下的底部结构。
  • STL 队列也不被归类为容器,而被归类为container adapter( 容器适配器)

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

std::queue<int, std::list<int>> third; // 定义以list为底层容器的队列

2. 用栈实现队列

代码随想录: 原文

力扣题目 : 232.用栈实现队列

图片-1682650378622

2.1 思路

  • 使用栈来模式队列的行为,需要两个栈一个输入栈,一个输出栈
  • push数据的时候,只要数据放进输入栈就好
  • pop的时候,输出栈如果为空,就把进栈(输入栈)数据全部导入进来,再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了
  • 如果进栈和出栈都为空的话,说明模拟的队列为空

代码实现

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    /** Initialize your data structure here. */
    MyQueue() {

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

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
        if (stOut.empty()) {
            // 从stIn导入数据直到stIn为空
            while(!stIn.empty()) {
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }

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

    /** Returns whether the queue is empty. */
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};
  • peek()的实现,直接复用了pop(),否则,对stOut判空的逻辑又要重写一遍
  • 工业级别代码开发中,一定要懂得复用,功能相近的函数要抽象出来,不要大量的复制粘贴。

3. 用队列实现栈

代码随想录: 原文

力扣题目 : 225. 用队列实现栈

图片-1682650826605

3.1 思路

注意: 题目强调是单向队列

两个队列实现栈

  • 队列是先进先出的规则,如果使用两个队列来实现栈,把一个队列中的数据导入另一个队列中,数据的顺序并没有变,并没有变成先进后出的顺序
  • 用两个队列que1和que2实现队列的功能,que2其实完全就是一个备份的作用
  • que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1
  • 这样数据的顺序就改变了

模拟的队列执行语句如下:

queue.push(1);        
queue.push(2);        
queue.pop();   // 注意弹出的操作       
queue.push(3);        
queue.push(4);       
queue.pop();  // 注意弹出的操作    
queue.pop();    
queue.pop();    
queue.empty();    

一个队列实现栈

一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序

3.1 代码实现

两个队列实现栈:

class MyStack {
public:
    queue<int> que1;
    queue<int> que2; // 辅助队列,用来备份
    /** Initialize your data structure here. */
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        que1.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que1.size();
        size--;
        while (size--) { // 将que1 导入que2,但要留下最后一个元素
            que2.push(que1.front());
            que1.pop();
        }

        int result = que1.front(); // 留下的最后一个元素就是要返回的值
        que1.pop();
        que1 = que2;            // 再将que2赋值给que1
        while (!que2.empty()) { // 清空que2
            que2.pop();
        }
        return result;
    }

    /** Get the top element. */
    int top() {
        return que1.back();
    }

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

一个队列实现栈:

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

    }
    /** Push element x onto stack. */
    void push(int x) {
        que.push(x);
    }
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que.size();
        size--;
        while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
            que.push(que.front());
            que.pop();
        }
        int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
        que.pop();
        return result;
    }

    /** Get the top element. */
    int top() {
        return que.back();
    }

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

4. 总结

  • 要了解编程语言里栈和队列的底层实现问题
  • 功能相近的函数要抽象出来,抽象成一个好用的函数或者工具类

学习时间:100min

0

评论区