image.pngimage.png

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. @ToString
    5. public class HeroNode {
    6. private int no;//排名
    7. private String name; //名称
    8. private String nickname; //别名
    9. private HeroNode next; //指向下一个节点
    10. public HeroNode(int no, String name, String nickname) {
    11. this.no = no;
    12. this.name = name;
    13. this.nickname = nickname;
    14. }
    15. }
    1. //单向链表
    2. public class SingleLinkedList {
    3. //创建头结点
    4. private static HeroNode head = new HeroNode(1,"宋江","及时雨",null);
    5. /**
    6. * 有序添加; 退添加的节点进行排序
    7. * @param newNode 被添加的节点
    8. * @return
    9. */
    10. public boolean addNodeOrder(HeroNode newNode){return addNodeOrderRecursion(head,newNode);}
    11. private static boolean addNodeOrderRecursion(HeroNode node,HeroNode newNode){
    12. if(node.getNext() != null){
    13. //编号重复
    14. if(node.getNo() == newNode.getNo()){
    15. System.out.println("编号重复");
    16. return false;
    17. }
    18. //排序
    19. if(node.getNo()<newNode.getNo() && newNode.getNo()<node.getNext().getNo()){
    20. newNode.setNext(node.getNext());
    21. node.setNext(newNode);
    22. }else{
    23. addNodeOrderRecursion(node.getNext(),newNode);
    24. }
    25. }else{
    26. node.setNext(newNode);
    27. }
    28. return true;
    29. }
    30. /**
    31. * 不排序添加;疯狂往后面扔
    32. * @param newNode 被添加的节点
    33. * @return
    34. */
    35. public boolean addNode(HeroNode newNode){return addNodeRecursion(head,newNode);}
    36. private static boolean addNodeRecursion(HeroNode node,HeroNode newNode){
    37. if(node.getNext() != null){
    38. //编号重复
    39. if(node.getNo() == newNode.getNo()){
    40. System.out.println("编号重复");
    41. return false;
    42. }
    43. addNodeRecursion(node.getNext(),newNode);
    44. }else{
    45. node.setNext(newNode);
    46. }
    47. return true;
    48. }
    49. /**
    50. * 遍历所有节点
    51. */
    52. public void nodeList(){nodeListRecursion(head);}
    53. private void nodeListRecursion(HeroNode node){
    54. System.out.print("排名: "+node.getNo());
    55. System.out.print(" 名字:\t"+node.getName());
    56. System.out.println(" 外号:\t"+node.getNickname());
    57. if(node.getNext() != null){
    58. nodeListRecursion(node.getNext());
    59. }else{
    60. return;
    61. }
    62. }
    63. /**
    64. * 根据 no 修改
    65. * @param heroNode
    66. * @return
    67. */
    68. public boolean updateNode(HeroNode heroNode){ return updateNodeRecuresion(head,heroNode);}
    69. private boolean updateNodeRecuresion(HeroNode node , HeroNode heroNode){
    70. ///假设当前的节点满足
    71. if(node.getNo() == heroNode.getNo()){
    72. node.setName(heroNode.getName());
    73. node.setNickname(heroNode.getNickname());
    74. System.out.println("修改成功");
    75. return true;
    76. }
    77. if(node.getNext() != null){
    78. return updateNodeRecuresion(node.getNext(),heroNode);
    79. }else{
    80. System.out.println("没有找到该节点");
    81. return false;
    82. }
    83. }
    84. /**
    85. * 根据 no 删除
    86. * @param no
    87. * @return
    88. */
    89. public boolean delteNode(int no){ return delteNodeRecuresion(head,no);}
    90. private boolean delteNodeRecuresion(HeroNode node,int no){
    91. if(node.getNext() !=null && node.getNext().getNo() == no){
    92. node.setNext(node.getNext().getNext());
    93. System.out.println("删除成功");
    94. return true;
    95. }
    96. if(node.getNext() != null){
    97. return delteNodeRecuresion(node.getNext(),no);
    98. }else {
    99. System.out.printf("未找到编号 %d 的节点",no);
    100. System.out.println();
    101. return false;
    102. }
    103. }
    104. /**
    105. * 通过 no 获取
    106. * @param no
    107. * @return
    108. */
    109. public HeroNode selectNode(int no){ return selectNodeRecuresion(head,no);}
    110. private HeroNode selectNodeRecuresion(HeroNode heroNode,int no){
    111. if(heroNode.getNo() == no){
    112. return heroNode;
    113. }
    114. if(heroNode.getNext() !=null){
    115. return selectNodeRecuresion(heroNode.getNext(),no);
    116. }else{
    117. System.out.printf("为找到编号为 %s 的节点",no);
    118. return null;
    119. }
    120. }
    121. /**
    122. * 获取倒数第n的节点
    123. * @param index
    124. * @return
    125. */
    126. public HeroNode reciprocalNode(int index){if(index==0) index=1;return reciprocalNodeRecuresion(head,nodeCount()-index);}
    127. private HeroNode reciprocalNodeRecuresion(HeroNode heroNode,int num){
    128. if(num<0){return null;}
    129. if(num != 0){
    130. num--;
    131. return reciprocalNodeRecuresion(heroNode.getNext(),num);
    132. }else{
    133. return heroNode;
    134. }
    135. }
    136. /**
    137. * 获取节点个数
    138. * @return
    139. */
    140. public int nodeCount(){ return nodeCountRecuresion(head,0);}
    141. private int nodeCountRecuresion(HeroNode heroNode,int count){
    142. if(heroNode.getNext() != null){
    143. count++;
    144. return nodeCountRecuresion(heroNode.getNext(),count);
    145. }else{
    146. count++;
    147. return count;
    148. }
    149. }
    150. /**
    151. * 反转
    152. * @return
    153. */
    154. public HeroNode reversal(){reversalRecuresion(head);return head;}
    155. private static void reversalRecuresion(HeroNode heroNode){
    156. if (heroNode.getNext() != null){
    157. reversalRecuresion(heroNode.getNext());
    158. heroNode.setNext(null);
    159. addNodeRecursion(head,heroNode);
    160. }else{
    161. head = heroNode;
    162. return;
    163. }
    164. }
    165. }
    public class Test {
    
        public static void main(String[] args) {
            SingleLinkedList singleLinkedList = new SingleLinkedList();
    
            HeroNode ljy = new HeroNode(2, "卢俊义", "玉麒麟");
            HeroNode wy = new HeroNode(3, "吴用", "智多星");
            HeroNode zs = new HeroNode(4, "张三", "路人甲");
            HeroNode ls = new HeroNode(5, "李四", "路人乙");
            HeroNode zl = new HeroNode(6, "赵六", "路人丙");
    
    
            //添加
            singleLinkedList.addNodeOrder(ljy);
            singleLinkedList.addNodeOrder(ls);
            singleLinkedList.addNodeOrder(wy);
            singleLinkedList.addNodeOrder(zs);
            singleLinkedList.addNodeOrder(zl);
    
            //修改
            HeroNode  lbw= new HeroNode(2, "卢本伟", "伞兵一号");
            singleLinkedList.updateNode(lbw);
    
            //通过 no 删除
            singleLinkedList.delteNode(5);
    
            //倒叙
            singleLinkedList.reversal();
    
            //遍历
            singleLinkedList.nodeList();
    
            //通过编号查询
            HeroNode selectNode = singleLinkedList.selectNode(2);
            System.out.println("----------------");
            System.out.printf("排名: %d\t名字: %s\t外号:%s",selectNode.getNo(),selectNode.getName(),selectNode.getNickname());
            System.out.println();
    
            //获取倒叙第 n 个节点
            HeroNode reciprocalNode = singleLinkedList.reciprocalNode(0);
            System.out.println("----------------");
            System.out.printf("排名: %d\t名字: %s\t外号:%s",reciprocalNode.getNo(),reciprocalNode.getName(),reciprocalNode.getNickname());
            System.out.println();
    
            //获取节点个数
            System.out.println(singleLinkedList.nodeCount());
    
        }
    }