栈/队列 互相模拟实现

举报
兔老大 发表于 2021/04/22 01:38:19 2021/04/22
【摘要】 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 思路:大概这么想:用一个辅助栈把进第一个栈的元素倒一下就好了。 比如进栈1,2,3,4,5 第一个栈: 5 4 3 2 1 然后倒到第二个栈里 1 2 3 4 5 再倒出来,顺序为1,2,3,4,5 实现队列 然后要注意的事情: 1)栈2非空不能往里面倒数...

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

思路:大概这么想:用一个辅助栈把进第一个栈的元素倒一下就好了。

比如进栈1,2,3,4,5

第一个栈:

5

4

3

2

1

然后倒到第二个栈里

1

2

3

4

5

再倒出来,顺序为1,2,3,4,5

实现队列

然后要注意的事情:

1)栈2非空不能往里面倒数,顺序就错了。栈2没数再从栈1倒。

2)栈1要倒就一次倒完,不倒完的话,进新数也会循序不对。


  
  1. import java.util.Stack;
  2. public class Solution {
  3. Stack<Integer> stack1 = new Stack<Integer>();
  4. Stack<Integer> stack2 = new Stack<Integer>();
  5. public void push(int node) {
  6. stack1.push(node);
  7. }
  8. public int pop() {
  9. if(stack1.empty()&&stack2.empty()){
  10. throw new RuntimeException("Queue is empty!");
  11. }
  12. if(stack2.empty()){
  13. while(!stack1.empty()){
  14. stack2.push(stack1.pop());
  15. }
  16. }
  17. return stack2.pop();
  18. }
  19. }

 

用两个队列实现栈,要求同上:

这其实意义不是很大,有些数据结构书上甚至说两个队列不能实现栈。

其实是可以的,只是时间复杂度较高,一个弹出操作时间为O(N)。

思路:两个队列,编号为1和2.

进栈操作:进1号队列

出栈操作:把1号队列全弄到2号队列里,剩最后一个别压入,而是返回。

最后还得把1和2号换一下,因为现在是2号有数,1号空。

 

仅仅有思考价值,不实用。

比如压入1,2,3

队列1:1,2,3

队列2:空

依次弹出1,2,3:

队列1里的23进入2号,3弹出

队列1:空

队列2:2,3

 

队列2中3压入1号,2弹出

队列1:3

队列2:空

 

队列1中只有一个元素,弹出。

 

上代码:


  
  1. public class TwoQueueImplStack {
  2. Queue<Integer> queue1 = new ArrayDeque<Integer>();
  3. Queue<Integer> queue2 = new ArrayDeque<Integer>();
  4. //压入
  5. public void push(Integer element){
  6. //都为空,优先1
  7. if(queue1.isEmpty() && queue2.isEmpty()){
  8. queue1.add(element);
  9. return;
  10. }
  11. //1为空,2有数据,放入2
  12. if(queue1.isEmpty()){
  13. queue2.add(element);
  14. return;
  15. }
  16. //2为空,1有数据,放入1
  17. if(queue2.isEmpty()){
  18. queue1.add(element);
  19. return;
  20. }
  21. }
  22. //弹出
  23. public Integer pop(){
  24. //两个都空,异常
  25. if(queue1.isEmpty() && queue2.isEmpty()){
  26. try{
  27. throw new Exception("satck is empty!");
  28. }catch(Exception e){
  29. e.printStackTrace();
  30. }
  31. }
  32. //1空,2有数据,将2中的数据依次放入1,最后一个元素弹出
  33. if(queue1.isEmpty()){
  34. while(queue2.size() > 1){
  35. queue1.add(queue2.poll());
  36. }
  37. return queue2.poll();
  38. }
  39. //2空,1有数据,将1中的数据依次放入2,最后一个元素弹出
  40. if(queue2.isEmpty()){
  41. while(queue1.size() > 1){
  42. queue2.add(queue1.poll());
  43. }
  44. return queue1.poll();
  45. }
  46. return (Integer)null;
  47. }
  48. //测试
  49. public static void main(String[] args) {
  50. TwoQueueImplStack qs = new TwoQueueImplStack();
  51. qs.push(2);
  52. qs.push(4);
  53. qs.push(7);
  54. qs.push(5);
  55. System.out.println(qs.pop());
  56. System.out.println(qs.pop());
  57. qs.push(1);
  58. System.out.println(qs.pop());
  59. }
  60. }

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/82740895

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。