当前位置:   article > 正文

数据结构:用Java实现双端队列(超详细)_java double-ended queue

java double-ended queue

Java双端队列

典型的双端队列收集如下所示:

Java中的双端队列收集

 双端队列(Deque:double ended queue)就是一个两端都是结尾的队列。队列的每一端都可以插入数据项和移除数据项。相对于普通队列,双端队列的入队和出队操作在两端都可进行。

 

left:左端    right:右端

这里我们使用最常用的顺序结构来存储双端队列,为了节省空间,把它首尾相连,构成循环队列。并且规定left指向左端的第一个元素,right指向右端的下一个位置。那么队空的判断则是left==right,队满是(left-1+MAX)%MAX==right或者(right-left+MAX)%MAX==MAX。
 

创建实现双端队列方法类:

  1. class ListNode {
  2. //数据域
  3. public int val;
  4. //指针
  5. public ListNode next;
  6. //初始化值
  7. public ListNode(int val) {
  8. this.val = val;
  9. }
  10. }
  11. public class Deque {
  12. public ListNode head;//头结点
  13. public ListNode last;//尾节点
  14. //在双端队列头那边添加节点变成新的头结点
  15. //在第一个节点处添加一个节点
  16. public void addFirst(int val) {
  17. //创建对象初始化值建立新节点
  18. ListNode node = new ListNode(val);
  19. //判断尾节点是否为空
  20. if (this.last == null) {
  21. //若为空就是头结点尾节点都是这个新创建的节点
  22. this.head = node;
  23. this.last = node;
  24. }
  25. //node成为新的头节点
  26. node.next = this.head;
  27. this.head = node;
  28. }
  29. //在双端队列尾那边添加节点变成新的尾节点
  30. //在节点的最后添加一个节点
  31. public void addLast(int val) {
  32. //创建对象初始化值建立新节点
  33. ListNode node = new ListNode(val);
  34. //判断尾节点是否为空
  35. if (this.last == null) {
  36. //若为空就是头结点尾节点都是这个新创建的节点
  37. this.head = node;
  38. this.last = node;
  39. }
  40. //node成为新的尾节点
  41. this.last.next = node;
  42. this.last = node;
  43. }
  44. //从头结点那边拿出Deque的一个节点
  45. public int offerFirst() {
  46. //判断头节点是否为空,如果是就输出!
  47. if (this.head == null) {
  48. System.out.println("!");
  49. return -1;
  50. }
  51. //如果不为空,把头结点指向的值拿出来
  52. int oldValue = this.head.val;
  53. //判断头结点尾节点是否重合,如果重合就表明双端队列为空
  54. if (this.head == this.last) {
  55. this.head = null;
  56. this.last = null;
  57. } else {
  58. //没有重合就接着找下一个节点变成新的头结点
  59. this.head = this.head.next;
  60. }
  61. return oldValue;
  62. }
  63. //从尾结点那边拿出Deque的一个节点
  64. public int offerLast() {
  65. //判断尾节点是否为空,如果就输出!
  66. if (this.last == null) {
  67. System.out.println("!");
  68. return -1;
  69. }
  70. // //如果不为空,把尾结点指向的值拿出来
  71. int oldValue = this.last.val;
  72. //判断头结点尾节点是否重合,如果重合就表明双端队列为空
  73. if (this.head == this.last) {
  74. this.last = null;
  75. this.head = null;
  76. } else {
  77. //遍历找到新的尾节点
  78. ListNode cur = this.head;
  79. while (cur.next != last) {
  80. cur = cur.next;
  81. }
  82. //把找到的最后一个节点做为尾节点
  83. this.last = cur;
  84. //尾节点.next=null
  85. this.last.next = null;
  86. }
  87. return oldValue;
  88. }
  89. //获取Deque处第一个节点的值
  90. public int peekFirst() {
  91. //判断头结点是否为空,是就输出!
  92. if (this.head == null) {
  93. System.out.println("!");
  94. return -1;
  95. }
  96. //返回头结点值
  97. return this.head.val;
  98. }
  99. //获取Deque上最后一个节点的值
  100. public int peekLast() {
  101. //判断尾结点是否为空,是就输出!
  102. if (this.last == null) {
  103. System.out.println("!");
  104. return -1;
  105. }
  106. //返回尾结点值
  107. return this.last.val;
  108. }
  109. //Check whether the Deque is empty
  110. public boolean empty() {
  111. return this.head == null;
  112. }
  113. public void display(){
  114. ListNode cur =head;
  115. while (cur!=last) {
  116. System.out.println(cur.val);
  117. cur = cur.next;
  118. }
  119. System.out.println(cur.val);
  120. }
  121. }

创建测试类:

  1. public class DequeTest {
  2. public static void main(String[] args) {
  3. Deque deque=new Deque();
  4. deque.addFirst(1);
  5. deque.addFirst(2);
  6. deque.addFirst(3);
  7. deque.display();
  8. deque.offerFirst();
  9. System.out.println();
  10. deque.display();
  11. }
  12. }

测试结果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/78790
推荐阅读
相关标签
  

闽ICP备14008679号