当前位置:   article > 正文

有序链表的基本用法

有序链表

有序链表:链表本身是一种无序的数据结构,元素的插入和删除不能保证顺序性,但是有没有有序的链表呢?答案是肯定的,我们在单链表中插入元素时,只需要将插入的元素与头结点及其后面的结点比较,从而找到合适的位置插入即可。一般在大多数需要使用有序数组的场合也可以使用有序链表,有序链表在插入时因为不需要移动元素,因此插入速度比数组快很多,另外链表可以扩展到全部有效的使用内存,而数组只能局限于一个固定的大小中。

        有序链表的Java代码实现:

  1. package parking;
  2. import java.util.Random;
  3. class Node {
  4. Integer data;
  5. Node next;
  6. public Node(Integer data) {
  7. this.data = data;
  8. }
  9. }
  10. public class LinkOrder {
  11. private Node head;
  12. private int size;
  13. public LinkOrder() {
  14. this.head = null;
  15. this.size = 0;
  16. }
  17. // 判断是否为空
  18. private boolean isEmpty() {
  19. return head == null ? true : false;
  20. }
  21. // 获取链表大小
  22. private int getSize() {
  23. return size;
  24. }
  25. // 在链表中插入一个结点,保持链表有序性(头结点最小,尾结点最大)
  26. public void insert(int key) {
  27. Node newLink = new Node(key);
  28. Node previous = null;
  29. Node current = head;
  30. while (current != null && key > current.data) { // 找下个节点
  31. previous = current;
  32. current = current.next;
  33. }
  34. // 比当前节点值小,刚好插入当前节点前面
  35. if (previous == null) {// 链表是空的
  36. head = newLink;
  37. } else {
  38. previous.next = newLink;
  39. }
  40. newLink.next = current;
  41. size++;
  42. }
  43. // 返回头结点
  44. private Integer headNode() {
  45. if (head == null) {
  46. return null;
  47. }
  48. Integer value = head.data;
  49. return value;
  50. }
  51. // 删除头结点,并删除
  52. private Integer deleteHnode() {
  53. if (head == null) {
  54. return null;
  55. }
  56. Integer value = head.data;
  57. if (head.next == null) {
  58. head = null;
  59. } else {
  60. head = head.next;
  61. }
  62. size--;
  63. return value;
  64. }
  65. // 删除指定结点
  66. private void deleteNode(Node node) {
  67. if (head == null) {
  68. System.out.println("链表为空");
  69. return;
  70. }
  71. Node current = head;
  72. Node pre = null;
  73. while (current.data != node.data) {
  74. if (current.next == null) {
  75. System.out.println("没有找到该结点");
  76. return;
  77. }
  78. pre = current;
  79. current = current.next;
  80. }
  81. if (current == head) {
  82. deleteHnode();
  83. } else {
  84. pre.next = current.next;
  85. size--;
  86. }
  87. }
  88. // 遍历有序链表
  89. private void sysNode() {
  90. if (head == null) {
  91. System.out.println("该链表为空");
  92. }
  93. Node current = head;
  94. while (current != null) {
  95. System.out.print(current.data + "-->");
  96. current = current.next;
  97. }
  98. System.out.println();
  99. }
  100. public static void main(String[] args) {
  101. // TODO Auto-generated method stub
  102. LinkOrder order = new LinkOrder();
  103. int i;
  104. Node node;
  105. for (i = 0; i < 5; i++) {
  106. order.insert(new Random().nextInt(100));
  107. }
  108. System.out.println("有序链表大小:" + order.getSize());
  109. order.sysNode();
  110. System.out.println("有序链表头结点:" + order.deleteHnode());
  111. order.sysNode();
  112. node = new Node(new Random().nextInt(100));
  113. System.out.println("删除指定结点:" + node.data);
  114. order.deleteNode(node);
  115. order.sysNode();
  116. }
  117. }

效果:

  1. 有序链表大小:5
  2. 44-->44-->59-->70-->71-->
  3. 有序链表头结点:44
  4. 44-->59-->70-->71-->
  5. 删除指定结点:59
  6. 44-->70-->71-->

 

我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。

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

闽ICP备14008679号