1. /**
    2. * @author chenshun00@gmail.com
    3. * @since 2021/7/25 10:03 上午
    4. */
    5. public class TestLinkedList<E> {
    6. private static class Node<E> {
    7. public Node<E> prev;
    8. public Node<E> next;
    9. private E data;
    10. }
    11. private int location;
    12. private final Node<E> head;
    13. private final Node<E> tail;
    14. public TestLinkedList() {
    15. head = new Node<E>();
    16. tail = new Node<E>();
    17. head.next = tail;
    18. head.prev = tail;
    19. tail.next = head;
    20. tail.prev = head;
    21. location = 0;
    22. }
    23. public int size() {
    24. return location;
    25. }
    26. public void add(E data) {
    27. Node<E> temp = new Node<>();
    28. //1、获取尾节点的前一个节点
    29. final Node<E> prev = tail.prev;
    30. //2、修改尾部指向
    31. prev.next = temp;
    32. tail.prev = temp;
    33. //3、修改当前节点指向
    34. temp.next = tail;
    35. temp.prev = prev;
    36. temp.data = data;
    37. location = location + 1;
    38. }
    39. public void add(int index, E data) {
    40. //1、找到下标节点
    41. Node<E> node = getNode(index);
    42. if (node == null) {
    43. add(data);
    44. return;
    45. }
    46. //找到前一个节点
    47. node = node.prev;
    48. //2、修改指向
    49. Node<E> temp = new Node<>();
    50. temp.data = data;
    51. //
    52. temp.prev = node;
    53. temp.next = node.next;
    54. //2.1
    55. node.next = temp;
    56. location = location + 1;
    57. }
    58. public boolean contains(E data) {
    59. Node<E> next = head.next;
    60. while (!next.equals(tail)) {
    61. final E nodeData = next.data;
    62. if (data.equals(nodeData)) {
    63. return true;
    64. }
    65. next = next.next;
    66. }
    67. return false;
    68. }
    69. public boolean remove(E data) {
    70. final Node<E> node = getNode(data);
    71. if (node == null) {
    72. return false;
    73. }
    74. //前一个节点的下一个修改为当前节点的下一个
    75. node.prev.next = node.next;
    76. //下一个节点的前一个修改为当前节点的前一个
    77. node.next.prev = node.prev;
    78. location = location - 1;
    79. return true;
    80. }
    81. public E get(int index) {
    82. final Node<E> node = getNode(index);
    83. return node == null ? null : node.data;
    84. }
    85. /**
    86. * 可以用二分法优化
    87. *
    88. * @param index 下标索引
    89. * @return node或者null
    90. */
    91. private Node<E> getNode(int index) {
    92. Node<E> next = head.next;
    93. for (int i = 0; i < location; i++) {
    94. if (i == index) {
    95. return next;
    96. } else {
    97. next = next.next;
    98. }
    99. }
    100. return null;
    101. }
    102. private Node<E> getNode(E data) {
    103. Node<E> next = head.next;
    104. while (!next.equals(tail)) {
    105. final E nodeData = next.data;
    106. if (data.equals(nodeData)) {
    107. return next;
    108. }
    109. next = next.next;
    110. }
    111. return null;
    112. }
    113. public static void main(String[] args) {
    114. TestLinkedList<String> testLinkedList = new TestLinkedList<>();
    115. for (int i = 0; i < 1024; i++) {
    116. testLinkedList.add(i + "zz");
    117. }
    118. System.out.println(testLinkedList.size());
    119. testLinkedList.add(1, "chenshun00");
    120. System.out.println(testLinkedList.get(1));
    121. System.out.println(testLinkedList.size());
    122. System.out.println(testLinkedList.contains("chenshun00"));
    123. System.out.println(testLinkedList.remove("chenshun00"));
    124. System.out.println(testLinkedList.size());
    125. System.out.println(testLinkedList.get(1));
    126. }
    127. }