• 【Java数据结构】详解Stack与Queue(四)


    🔒文章目录:

    1.❤️❤️前言~🥳🎉🎉🎉

    2.用队列实现栈 

    3.用栈实现队列

    4.栈和队列存放null

    5.总结 


    1.❤️❤️前言~🥳🎉🎉🎉

    Hello, Hello~ 亲爱的朋友们👋👋,这里是E绵绵呀✍️✍️。

    如果你喜欢这篇文章,请别吝啬你的点赞❤️❤️和收藏📖📖。如果你对我的内容感兴趣,记得关注我👀👀以便不错过每一篇精彩。

    当然,如果在阅读中发现任何问题或疑问,我非常欢迎你在评论区留言指正🗨️🗨️。让我们共同努力,一起进步!

    加油,一起CHIN UP!💪💪

    🔗个人主页:E绵绵的博客
    📚所属专栏:

    1. JAVA知识点专栏

            深入探索JAVA的核心概念与技术细节

    2.JAVA题目练习

            实战演练,巩固JAVA编程技能

    3.c语言知识点专栏

            揭示c语言的底层逻辑与高级特性

    4.c语言题目练习

            挑战自我,提升c语言编程能力

    📘 持续更新中,敬请期待❤️❤️ 

    这篇文章我们将给大家带来队列和栈的两道练习题,帮助大家更好应用队列和栈。

    借鉴文章 :【Java---数据结构】队列-CSDN博客

      

    2.用队列实现栈 

    📌题目描述:

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

      实现 MyStack 类:

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

    你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
    你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

    一个空的栈不会调用pop和top。

    📋题目示例 

    输入:
    ["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 

    ⏳解题思路  

    栈:先进后出
    队列:先进先出


    创建两个队列,分别为队列1、队列2。无论出栈还是入栈都操作的是不为空的队列。


    元素入栈时,将元素存放到不为空的队列中。一开始两个队列都为空,那么就指定其中一个队列进行入队操作。


    元素出栈时,找到不为空的队列,将队列中size-1个元素先转移到另一个队列中(转移:通过遍历队列,将出队的每一个元素先存放到一个变量中,再将该变量插入到另外一个队列中),剩下的一个元素就是要出栈的元素,所以将剩下的一个进行出队操作。


    获取栈顶元素时,将队列中size个元素先转移到另一个队列中,返回保存转移元素的变量。(最终保存的是队列的最后一个元素,即为栈顶元素)。

    当两个队列都为空时,此时可以判断出栈为空。

      代码示例 (包含测试模拟的栈功能是否实现的代码) 

    1. class MyStack {
    2. Queue<Integer> queue1;
    3. Queue<Integer> queue2;
    4. public MyStack() {
    5. queue1 =new LinkedList<>();
    6. queue2=new LinkedList<>();
    7. }
    8. public void push(int x) {
    9. if(!queue1.isEmpty())
    10. queue1.offer(x);
    11. else {
    12. if (!queue2.isEmpty())
    13. queue2.offer(x);
    14. else
    15. queue1.offer(x);
    16. }
    17. }
    18. public int pop() {
    19. if(!queue1.isEmpty()){
    20. while(queue1.size()!=1){
    21. queue2.offer(queue1.poll()) ;
    22. }
    23. return queue1.poll();
    24. }
    25. else {
    26. while(queue2.size()!=1){
    27. queue1.offer(queue2.poll()) ;
    28. }
    29. return queue2.poll();
    30. }
    31. }
    32. public int top() {
    33. if(!queue1.isEmpty()){
    34. while(queue1.size()!=1){
    35. queue2.offer(queue1.poll()) ;
    36. }
    37. int key= queue1.poll();
    38. queue2.offer(key);
    39. return key;
    40. }
    41. else {
    42. while(queue2.size()!=1){
    43. queue1.offer(queue2.poll()) ;
    44. }
    45. int key= queue2.poll();
    46. queue1.offer(key);
    47. return key;
    48. }
    49. }
    50. public boolean empty() {
    51. if(queue1.isEmpty()&&queue2.isEmpty())
    52. return true;
    53. else
    54. return false;
    55. }
    56. }
    57. //每次调用 pop 和 top 都保证栈不为空
    58. public class Test{
    59. public static void main(String[] args) {
    60. MyStack myStack=new MyStack();
    61. myStack.push(1);//入栈
    62. myStack.push(2);
    63. myStack.push(3);
    64. myStack.push(4);
    65. System.out.println(myStack.top());//获取栈顶元素
    66. System.out.println(myStack.pop());//出栈
    67. System.out.println(myStack.pop());
    68. System.out.println(myStack.pop());
    69. System.out.println(myStack.pop());
    70. System.out.println(myStack.empty());
    71. //判断栈是否为空,如果为空返回true,否则返回false
    72. }
    73. }


    该题链接:用队列实现栈

    3.用栈实现队列

    📌题目描述:

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

    实现 MyQueue 类:

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

    你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
    你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

    一个空的队列不会调用pop和top。

    📋题目示例

    输入:
    ["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

    ⏳解题思路 

    • 创建两个栈,分别为栈1、栈2。
    • 入队:将所有元素都存放到栈1里面
    • 出队:出队操作对栈2进行出栈,如果栈2为空,那么就把栈1里面的所有元素都放到栈2中。
    • 从栈1进,从栈2出。这样可以满足队列先进先出的特点。
    • 查看栈顶元素也同理,它跟出队一样,只不过出队要去除栈2的栈顶元素,这里不用去除。
    • 当两个栈都为空时,表示队列为空。

       代码示例 (包含测试模拟的队列功能是否实现的代码)

    1. class MyQueue {
    2. public Stack<Integer> stack1;
    3. public Stack<Integer> stack2;
    4. public MyQueue() {
    5. stack1=new Stack<>();
    6. stack2=new Stack<>();
    7. }
    8. public void push(int x) {
    9. stack1.push(x);
    10. }
    11. public int pop() {
    12. if(stack2.empty()){
    13. while(!stack1.empty())
    14. stack2.push(stack1.pop());
    15. }
    16. return stack2.pop();
    17. }
    18. public int peek() {
    19. if(stack2.empty()){
    20. while(!stack1.empty())
    21. stack2.push(stack1.pop());
    22. }
    23. return stack2.peek();
    24. }
    25. public boolean empty() {
    26. if(stack1.empty()&&stack2.empty())
    27. return true;
    28. else
    29. return false;
    30. }
    31. }
    32. // 一个空的队列不会调用 pop 或者 peek 操作
    33. public class Test1 {
    34. public static void main(String[] args) {
    35. MyQueue myQueue=new MyQueue();
    36. myQueue.push(4);
    37. myQueue.push(3);
    38. myQueue.push(2);
    39. myQueue.push(1);
    40. System.out.println(myQueue.peek());
    41. myQueue.pop();
    42. myQueue.pop();
    43. System.out.println(myQueue.peek());
    44. System.out.println(myQueue.empty());
    45. }
    46. }

    该题链接:用栈实现队列 

    4.栈和队列存放null

    栈和队列都允许存储null值。在栈和队列中,null值被视为一种有效的元素,因此可以被添加到栈和队列中,作为一个元素去存放。

    如下代码可以证明:

    1. public class Test1 {
    2. public static void main(String[] args) {
    3. Stack<Integer> stack=new Stack<>();//如下证明栈能存放null
    4. stack.push(null);
    5. System.out.println(stack.size());
    6. System.out.println(stack.peek());
    7. System.out.println(stack.pop());
    8. Queue<Integer> queue=new LinkedList<>();//如下证明队列能存放null
    9. queue.offer(null);
    10. System.out.println(queue.size());
    11. System.out.println(queue.peek());
    12. System.out.println(queue.poll());
    13. }
    14. }


    5.总结 

    至此我们就用了四篇文章把栈和队列讲完了,下篇文章将会给大家介绍二叉树。在此,我们诚挚地邀请各位大佬们为我们点赞、关注,并在评论区留下您宝贵的意见与建议。让我们共同学习,共同进步,为知识的海洋增添更多宝贵的财富!🎉🎉🎉❤️❤️💕💕🥳👏👏👏

  • 相关阅读:
    QtiPlot for Mac v1.1.3(科学数据分析工具)
    Java进阶知识复习笔记(三):函数式编程
    Python:AES+Base64的加密与解密(ECB模式)
    【Leetcode刷题Python】516. 最长回文子序列
    【STC8A8K64D4开发板】——STC8A8K64D4开发板介绍
    利用python批量读取大量Excel表格文件中指定内容并汇总
    jwt的了解和使用以及大致代码分析
    第四十一天&性能测试理论
    3. 递归
    k8s 读书笔记 - kubernetes 基本概念和术语(下)
  • 原文地址:https://blog.csdn.net/Easonmax/article/details/139042505