栈是先进后出,队列是先进后出,这里讨论一下两种数据结构之间的相互实现。

一.用两个栈实现队列

我们用一个栈来实现队列的进队操作(栈A),用另一个栈来实现队列的出队操作(栈B)。

1.入队列:

把元素放进栈A即可。假如栈A已满并且栈B为空,可以先把栈A中的所有元素先弹出并放入栈B中;假如栈B不为空,则出错了(不能插入)。

2.出队列:

假如栈B不为空,直接弹出。假如栈B为空,由于队列是先进先出的,因此要出队列时,我们要先把栈A中的元素全部放进栈B中,然后再从栈B中弹出栈顶元素。

3.例子:

进行以下操作:

(1)插入1:

栈A:1

栈B:空

(2)插入2(左边为栈顶):

栈A:2 1

栈B:空

(3)出队列:

栈B为空,先把栈A的元素弹出插入栈B:

栈A:空
栈B:1 2

栈B弹出:
栈A:空

栈B:2

(4)出队列

栈B不为空,直接弹出:
栈A:空

栈B:空

这样,进队列的顺序为1  2,出队列的顺序为1 2,满足队列的特性。

4.LeetCode相关题目

232. Implement Queue using Stacks(https://leetcode.com/problems/implement-queue-using-stacks/description/):

这道题的考虑的东西很少,没考虑一些特殊情况:

  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. class MyQueue {
  5. public:
  6. stack<int> in;
  7. stack<int> out;
  8. /** Initialize your data structure here. */
  9. MyQueue() {
  10. }
  11.  
  12. /** Push element x to the back of queue. */
  13. void push(int x) {
  14. in.push(x);
  15. }
  16.  
  17. /** Removes the element from in front of queue and returns that element. */
  18. int pop() {
  19. if (!out.empty()) {
  20. int temp = out.top();
  21. out.pop();
  22. return temp;
  23. }
  24. else {
  25. if (in.empty()) {
  26. return -;
  27. }
  28. else {
  29. while (!in.empty()) {
  30. out.push(in.top());
  31. in.pop();
  32. }
  33. int temp = out.top();
  34. out.pop();
  35. return temp;
  36. }
  37. }
  38. }
  39.  
  40. /** Get the front element. */
  41. int peek() {
  42. if (!out.empty()) {
  43. return out.top();
  44. }
  45. else {
  46. if (in.empty()) {
  47. return -;
  48. }
  49. else {
  50. while (!in.empty()) {
  51. out.push(in.top());
  52. in.pop();
  53. }
  54. return out.top();
  55. }
  56. }
  57. }
  58.  
  59. /** Returns whether the queue is empty. */
  60. bool empty() {
  61. return in.empty() && out.empty();
  62. }
  63. };
  64.  
  65. /**
  66. * Your MyQueue object will be instantiated and called as such:
  67. * MyQueue obj = new MyQueue();
  68. * obj.push(x);
  69. * int param_2 = obj.pop();
  70. * int param_3 = obj.peek();
  71. * bool param_4 = obj.empty();
  72. */

二.用两个队列实现栈:

1.用队列实现栈有两种方法,两种方法的入栈和出栈的时间复杂度不相同,按需使用:

假设有两个队列,一个队列A,不为空,一个队列B,为空。队列A中的元素符合栈操作的前提。
(1)入栈O(n),出栈(1)

入栈时,直接把元素放进空的队列B,然后把队列A所有的元素按顺序放到队列B中。队列A就变为空了。此时,最后一个“入栈”的元素就成了队列头,需要弹出直接从队列B中弹出即可。这样就满足了栈后进先出的特性了。之后的操作两个队列交替就行了。

(2)入栈O(1),出栈(n)

入栈时,直接把元素放进不为空的队列A的队尾;出栈时,把栈A的前n-1个元素放入栈B中,栈A中剩下一个的元素就是最新插入的元素,直接出队列,也满足栈的特性了。

2.LeetCode相关题目

225. Implement Stack using Queues(https://leetcode.com/problems/implement-stack-using-queues/description/)

这道题用第(1)种方法会快一点,说明出栈操作多一点吧。

方法(1):

  1. class MyStack {
  2. public:
  3. /** Initialize your data structure here. */
  4. queue<int> a;
  5. queue<int> b;
  6. MyStack() {
  7.  
  8. }
  9.  
  10. /** Push element x onto stack. */
  11. void push(int x) {
  12. if (a.empty() && b.empty()) {
  13. a.push(x);
  14. return;
  15. }
  16. if (a.empty()) {
  17. a.push(x);
  18. while (!b.empty()) {
  19. a.push(b.front());
  20. b.pop();
  21. }
  22. }
  23. else {
  24. b.push(x);
  25. while (!a.empty()) {
  26. b.push(a.front());
  27. a.pop();
  28. }
  29. }
  30. }
  31.  
  32. /** Removes the element on top of the stack and returns that element. */
  33. int pop() {
  34. if (a.empty()) {
  35. int temp = b.front();
  36. b.pop();
  37. return temp;
  38. }
  39. else {
  40. int temp = a.front();
  41. a.pop();
  42. return temp;
  43. }
  44. }
  45.  
  46. /** Get the top element. */
  47. int top() {
  48. return a.empty() ? b.front() : a.front();
  49. }
  50.  
  51. /** Returns whether the stack is empty. */
  52. bool empty() {
  53. return a.empty() && b.empty();
  54. }
  55. };

方法(2):

  1. class MyStack {
  2. public:
  3. /** Initialize your data structure here. */
  4. queue<int> a;
  5. queue<int> b;
  6. MyStack() {
  7.  
  8. }
  9.  
  10. /** Push element x onto stack. */
  11. void push(int x) {
  12. if (a.empty() && b.empty()) {
  13. a.push(x);
  14. return;
  15. }
  16. if (!a.empty()) {
  17. a.push(x);
  18. }
  19. if (!b.empty()) {
  20. b.push(x);
  21. }
  22. }
  23.  
  24. /** Removes the element on top of the stack and returns that element. */
  25. int pop() {
  26. if (!a.empty()) {
  27. while (a.size() != ) {
  28. b.push(a.front());
  29. a.pop();
  30. }
  31. int temp = a.front();
  32. a.pop();
  33. return temp;
  34. }
  35. else {
  36. while (b.size() != ) {
  37. a.push(b.front());
  38. b.pop();
  39. }
  40. int temp = b.front();
  41. b.pop();
  42. return temp;
  43. }
  44. }
  45.  
  46. /** Get the top element. */
  47. int top() {
  48. if (!a.empty()) {
  49. while (a.size() != ) {
  50. b.push(a.front());
  51. a.pop();
  52. }
  53. int temp = a.front();
  54. b.push(a.front());
  55. a.pop();
  56. return temp;
  57. }
  58. else {
  59. while (b.size() != ) {
  60. a.push(b.front());
  61. b.pop();
  62. }
  63. int temp = b.front();
  64. a.push(b.front());
  65. b.pop();
  66. return temp;
  67. }
  68. }
  69.  
  70. /** Returns whether the stack is empty. */
  71. bool empty() {
  72. return a.empty() && b.empty();
  73. }
  74. };

栈和队列数据结构的相互实现[LeetCode]的更多相关文章

  1. 栈和队列数据结构的基本概念及其相关的Python实现

    先来回顾一下栈和队列的基本概念: 相同点:从"数据结构"的角度看,它们都是线性结构,即数据元素之间的关系相同. 不同点:栈(Stack)是限定只能在表的一端进行插入和删除操作的线性 ...

  2. 九度OJ 1512 用两个栈实现队列 【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1512 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 输入: 每 ...

  3. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  4. 数据结构和算法(Golang实现)(14)常见数据结构-栈和队列

    栈和队列 一.栈 Stack 和队列 Queue 我们日常生活中,都需要将物品排列,或者安排事情的先后顺序.更通俗地讲,我们买东西时,人太多的情况下,我们要排队,排队也有先后顺序,有些人早了点来,排完 ...

  5. 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别

    前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...

  6. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

  7. 数据结构之栈和队列及其Java实现

    栈和队列是数据结构中非常常见和基础的线性表,在某些场合栈和队列使用很多,因此本篇主要介绍栈和队列,并用Java实现基本的栈和队列,同时用栈和队列相互实现. 栈:栈是一种基于“后进先出”策略的线性表.在 ...

  8. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  9. 详细分析栈和队列的数据结构的实现过程(Java 实现)

    目录 栈和队列的数据结构的实现过程(Java 实现) 栈的数据结构的实现 栈的基础知识回顾 栈的常见应用 基于数组的栈的实现 具体代码设计 基于数组的栈简单的时间复杂度分析 关于栈的一个算法应用:括号 ...

随机推荐

  1. 高性能分布式执行框架——Ray

    Ray是UC Berkeley AMP实验室新推出的高性能分布式执行框架,它使用了和传统分布式计算系统不一样的架构和对分布式计算的抽象方式,具有比Spark更优异的计算性能. Ray目前还处于实验室阶 ...

  2. Heap Sorting 总结 (C++)

    各位读者,大家好. 因为算法和数据结构相关的知识都是在国外学的,所以有些词汇翻译的可能不准确,然后一些源代码的注释可能是英文的,如有给大家带来什么不方便,请见谅.今天我想写一下Heap相关的知识,从基 ...

  3. spark2的编译

    0.操作系统 centos:6.4 hadoop:2.5.0-cdh5.3.6 1.为什么要编译 spark 源码? 学习spark的第一步 就应该是编译源码,后期修改和调试,扩展集成的功能模块 2. ...

  4. 2943:小白鼠排队-poj

    2943:小白鼠排队 总时间限制:  1000ms 内存限制:  65536kB 描述 N只小白鼠(1 < N < 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按 ...

  5. 用sort()按小到大排序的方法:

    例子:function compare(value1,value2){ if(value1<value2){ return -1; }else if(value1==value2){ retur ...

  6. PHP基础知识点

    //语法错误(syntax error)在语法分析阶段,源代码并未被执行,故不会有任何输出. /* [命名规则] */常量名 类常量建议全大写,单词间用下划线分隔 // MIN_WIDTH变量名建议用 ...

  7. HTTP协议之URL

    1.什么是URL URL的全称是Uniform Resoure Locator,统一资源定位器.URL是浏览器寻找信息时所需的资源位置.当一个人将浏览器指向一个URL,浏览器就会在幕后发送适当的协议报 ...

  8. Vue单页面骨架屏实践

    github 地址: VV-UI/VV-UI 演示地址: vv-ui 文档地址:skeleton 关于骨架屏介绍 骨架屏的作用主要是在网络请求较慢时,提供基础占位,当数据加载完成,恢复数据展示.这样给 ...

  9. WINDOWS java 不能正常卸载 问题, (其他系统问题 也可以试试)

    1.JAVA 原安装包无法卸载  不知道 有没有通知 碰到过这种情况的 自己碰到过3次这种情况了,    卸载不掉, 在网上 找了N多中 方法, 注册表什么的都被翻烂了, 单还是没用,其中有一次还把 ...

  10. db2 调整连接数的优化

    The Version 9.5 default for the max_coordagents and max_connections parameters will be AUTOMATIC, wi ...