• 单链表头尾指针


     容器接口

    1. import java.io.Serializable;
    2. import java.util.stream.Stream;
    3. public interface Container extends Serializable {
    4. /**
    5. * 求容器大小
    6. */
    7. int size();
    8. /**
    9. * 容器是否包含元素
    10. *
    11. * @param e 要判断的元素
    12. * @return true表示容器中存在该元素
    13. */
    14. boolean contains(E e);
    15. /**
    16. * 清空容器
    17. */
    18. void clear();
    19. /**
    20. * 容器是否为空
    21. *
    22. * @return true表示为空
    23. */
    24. default boolean isEmpty() {
    25. return size() == 0;
    26. }
    27. /**
    28. * 流,数据源不能被修改,所以要屏蔽所有修改方法??臃肿!
    29. */
    30. default Stream stream() {
    31. throw new UnsupportedOperationException();
    32. }
    33. }

     链表实现类

    1. /**
    2. * 单链表
    3. * 链表类:头指针、尾指针、链表大小
    4. *
    5. * @param
    6. */
    7. public class MyLinkedList implements Container {
    8. private Node head = new Node<>(null);
    9. private Node tail = head;
    10. private int size;
    11. public static void main(String[] args) {
    12. MyLinkedList list = new MyLinkedList<>();
    13. // 向单链表中插入元素
    14. list.addLast(1);
    15. list.addLast(2);
    16. list.addLast(3);
    17. list.addLast(4);
    18. list.addLast(5);
    19. list.addLast(6);
    20. list.addLast(7);
    21. System.out.println(list);
    22. // 移除第一个元素
    23. list.removeFirst();
    24. list.removeFirst();
    25. list.addFirst(2);
    26. System.out.println(list);
    27. // 移除最后一个元素
    28. list.removeLast();
    29. list.removeLast();
    30. list.addFirst(1);
    31. list.addLast(6);
    32. System.out.println(list);
    33. // 清空链表
    34. list.clear();
    35. System.out.println(list);
    36. list.addFirst(1);
    37. System.out.println(list);
    38. }
    39. /**
    40. * 在链表头插入,头尾指针互相不干涉,头指针永远指向头节点,尾指针永远指向尾节点
    41. *
    42. * @param e 插入的元素
    43. */
    44. public void addFirst(E e) {
    45. if (e == null) {
    46. throw new NullPointerException();
    47. }
    48. Node node = new Node<>(e);
    49. node.next = head.next;
    50. head.next = node;
    51. size++;
    52. }
    53. /**
    54. * 向链表尾部添加元素
    55. *
    56. * @param e 要添加的元素
    57. */
    58. public void addLast(E e) {
    59. if (e == null) {
    60. throw new NullPointerException();
    61. }
    62. size++;
    63. Node newTail = new Node<>(e);
    64. tail.next = newTail;
    65. tail = newTail;
    66. }
    67. /**
    68. * 移除链表最后一个元素
    69. */
    70. public void removeLast() {
    71. if (isEmpty()) {
    72. return;
    73. }
    74. size--;
    75. Node temp = head;
    76. while (temp.next != tail) {
    77. temp = temp.next;
    78. }
    79. temp.next.elem = null; // 删除最后一个元素
    80. temp.next = null; // 修改倒数第二个元素指针指向空
    81. tail = temp;
    82. }
    83. /**
    84. * 移除链表第一个元素
    85. */
    86. public void removeFirst() {
    87. if (isEmpty()) {
    88. return;
    89. }
    90. size--;
    91. head.next.elem = null;
    92. head = head.next;
    93. }
    94. @Override
    95. public int size() {
    96. return size;
    97. }
    98. private Node getNode(E e) {
    99. Node temp = head;
    100. while (temp.next != null) {
    101. temp = temp.next;
    102. if (e.equals(temp.elem)) {
    103. return temp;
    104. }
    105. }
    106. return null;
    107. }
    108. @Override
    109. public boolean contains(E e) {
    110. Node temp = head;
    111. while (temp.next != null) {
    112. temp = temp.next;
    113. if (e.equals(temp.elem)) {
    114. return true;
    115. }
    116. }
    117. return false;
    118. }
    119. @Override
    120. public void clear() {
    121. head.next = null;
    122. tail = head;
    123. size = 0;
    124. }
    125. @Override
    126. public String toString() {
    127. if (isEmpty()) {
    128. return "[]";
    129. }
    130. final StringBuilder sb = new StringBuilder();
    131. Node temp = head;
    132. while (temp.next != null) {
    133. temp = temp.next;
    134. sb.append(",").append(temp.elem);
    135. }
    136. sb.insert(1, "[").append("]");
    137. return sb.substring(1);
    138. }
    139. private static class Node {
    140. E elem;
    141. transient Node next;
    142. public Node(E elem) {
    143. this.elem = elem;
    144. }
    145. public Node(E elem, Node next) {
    146. this.elem = elem;
    147. this.next = next;
    148. }
    149. @Override
    150. public String toString() {
    151. return elem.toString();
    152. }
    153. }
    154. }

  • 相关阅读:
    [附源码]JAVA毕业设计个人交友网站(系统+LW)
    自定义View的绘制:Xfermode用法解析
    动物园IP网络广播-基于IP局域网络的动物园背景音乐广播系统-动物园智能广播系统设计指南
    【Vue3 源码解析】v-model 和 emit
    Vue2与Vue3:深度剖析核心差异与升级亮点
    【C语言】文件操作详解
    我的前端开发技巧
    设备树的引进与体验_使用设备树时的驱动编程
    css 实现渐变色字体
    postman下载安装汉化及使用
  • 原文地址:https://blog.csdn.net/zhangpeng455547940/article/details/128197800