image.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. private HeroNode pre; //指向上一个节点
    11. public HeroNode(int no, String name, String nickname) {
    12. this.no = no;
    13. this.name = name;
    14. this.nickname = nickname;
    15. }
    16. }
    1. //双向链表
    2. public class SingleLinkedList {
    3. HeroNode head = new HeroNode(1,"宋江","及时雨",null,null);
    4. //添加一个节点
    5. public boolean addNode(HeroNode newNode){return addNodeRecursion(head,newNode);}
    6. private static boolean addNodeRecursion(HeroNode node,HeroNode newNode){
    7. if(node.getNext() != null){
    8. return addNodeRecursion(node.getNext(),newNode);
    9. }else{
    10. newNode.setPre(node);
    11. node.setNext(newNode);
    12. return true;
    13. }
    14. }
    15. //有序添加
    16. public boolean addNodeOrder(HeroNode newNode){return addNodeOrderRecursion(head,newNode);}
    17. private boolean addNodeOrderRecursion(HeroNode node,HeroNode newNode){
    18. if(node.getNext() != null) {
    19. if (node.getNo() < newNode.getNo() && node.getNext().getNo() > newNode.getNo()) {
    20. //将newNode的 next 指向当前节点的 next
    21. newNode.setNext(node.getNext());
    22. //将newNode 的pre指向当前节点
    23. newNode.setPre(node);
    24. //将当前节点的 next的pre指向newNode
    25. node.getNext().setPre(newNode);
    26. //将当前节点的 next 指向 newNode
    27. node.setNext(newNode);
    28. return true;
    29. } else if (node.getNo() > newNode.getNo() && node.getPre()!=null && node.getPre().getNo() < newNode.getNo()) {
    30. //将newNode 的next指向当前
    31. newNode.setNext(node);
    32. //将newNode的pre指向当前节点的pre
    33. newNode.setPre(node.getPre());
    34. //将当前节点的pre指向newNode
    35. node.setPre(newNode);
    36. //将当前节点的pre的next指向newNode
    37. node.getPre().setNext(newNode);
    38. return true;
    39. }else if(node.getPre() == null && node.getNo()>newNode.getNo()){
    40. //将newNode 的next指向当前
    41. newNode.setNext(node);
    42. //将当前节点的pre指向newNode
    43. node.setPre(newNode);
    44. head = newNode;
    45. return true;
    46. }else{
    47. return addNodeOrderRecursion(node.getNext(),newNode);
    48. }
    49. }else{
    50. newNode.setPre(node);
    51. node.setNext(newNode);
    52. return true;
    53. }
    54. }
    55. //正序遍历
    56. public void nodeList(){nodeListRecursion(head);}
    57. private void nodeListRecursion(HeroNode node){
    58. System.out.print("排名: "+node.getNo());
    59. System.out.print(" 名字:\t"+node.getName());
    60. System.out.println(" 外号:\t"+node.getNickname());
    61. if(node.getNext() != null){
    62. nodeListRecursion(node.getNext());
    63. }else{
    64. return;
    65. }
    66. }
    67. //删除
    68. public Boolean deleteNode(int no){return delteNodeRecuresion(head,no);};
    69. private boolean delteNodeRecuresion(HeroNode node, int no) {
    70. if(node.getNo() == no){
    71. //把上一个节点 的 next指向当前节点的 next
    72. node.getPre().setNext(node.getNext());
    73. //把下一个节点 pre 指向当前节点的 pre
    74. node.getNext().setPre(node.getPre());
    75. return true;
    76. }
    77. if(node.getNext() != null){
    78. return delteNodeRecuresion(node.getNext(),no);
    79. }else{
    80. return false;
    81. }
    82. }
    83. }
    
    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, "赵六", "路人丙");
            HeroNode a = new HeroNode(-1, "赵六", "路人丙");
            HeroNode b = new HeroNode(0, "赵六", "路人丙");
    
            //添加
            singleLinkedList.addNodeOrder(zl);
            singleLinkedList.addNodeOrder(ls);
            singleLinkedList.addNodeOrder(wy);
            singleLinkedList.addNodeOrder(zs);
            singleLinkedList.addNodeOrder(ljy);
            singleLinkedList.addNodeOrder(a);
            singleLinkedList.addNodeOrder(b);
    
            //删除
            //singleLinkedList.deleteNode(6);
    
            System.out.println("-------------正序遍历--------------");
            singleLinkedList.nodeList();
    
    
    
        }
    }