
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @ToString
- public class HeroNode {
-     private int no;//排名
-     private String name; //名称
-     private String nickname; //别名
-     private HeroNode next;  //指向下一个节点
-     private HeroNode pre;   //指向上一个节点
-     public HeroNode(int no, String name, String nickname) {
-         this.no = no;
-         this.name = name;
-         this.nickname = nickname;
-     }
- }
- //双向链表
- public class SingleLinkedList {
-     HeroNode head = new HeroNode(1,"宋江","及时雨",null,null);
-     //添加一个节点
-     public boolean addNode(HeroNode newNode){return addNodeRecursion(head,newNode);}
-     private static boolean addNodeRecursion(HeroNode node,HeroNode newNode){
-         if(node.getNext() != null){
-             return addNodeRecursion(node.getNext(),newNode);
-         }else{
-             newNode.setPre(node);
-             node.setNext(newNode);
-             return true;
-         }
-     }
-     //有序添加
-     public boolean addNodeOrder(HeroNode newNode){return addNodeOrderRecursion(head,newNode);}
-     private boolean addNodeOrderRecursion(HeroNode node,HeroNode newNode){
-         if(node.getNext() != null) {
-             if (node.getNo() < newNode.getNo() && node.getNext().getNo() > newNode.getNo()) {
-                 //将newNode的 next 指向当前节点的 next
-                 newNode.setNext(node.getNext());
-                 //将newNode 的pre指向当前节点
-                 newNode.setPre(node);
-                 //将当前节点的 next的pre指向newNode
-                 node.getNext().setPre(newNode);
-                 //将当前节点的 next 指向 newNode
-                 node.setNext(newNode);
-                 return true;
-             } else if (node.getNo() > newNode.getNo() && node.getPre()!=null && node.getPre().getNo() < newNode.getNo()) {
-                 //将newNode 的next指向当前
-                 newNode.setNext(node);
-                 //将newNode的pre指向当前节点的pre
-                 newNode.setPre(node.getPre());
-                 //将当前节点的pre指向newNode
-                 node.setPre(newNode);
-                 //将当前节点的pre的next指向newNode
-                 node.getPre().setNext(newNode);
-                 return true;
-             }else if(node.getPre() == null && node.getNo()>newNode.getNo()){
-                 //将newNode 的next指向当前
-                 newNode.setNext(node);
-                 //将当前节点的pre指向newNode
-                 node.setPre(newNode);
-                 head = newNode;
-                 return true;
-             }else{
-                 return addNodeOrderRecursion(node.getNext(),newNode);
-             }
-         }else{
-             newNode.setPre(node);
-             node.setNext(newNode);
-             return true;
-         }
-     }
-     //正序遍历
-     public void nodeList(){nodeListRecursion(head);}
-     private  void nodeListRecursion(HeroNode node){
-         System.out.print("排名: "+node.getNo());
-         System.out.print("  名字:\t"+node.getName());
-         System.out.println("  外号:\t"+node.getNickname());
-         if(node.getNext() != null){
-             nodeListRecursion(node.getNext());
-         }else{
-             return;
-         }
-     }
-     //删除
-     public Boolean deleteNode(int no){return delteNodeRecuresion(head,no);};
-     private boolean delteNodeRecuresion(HeroNode node, int no) {
-         if(node.getNo() == no){
-             //把上一个节点 的 next指向当前节点的 next
-             node.getPre().setNext(node.getNext());
-             //把下一个节点 pre 指向当前节点的 pre
-             node.getNext().setPre(node.getPre());
-             return true;
-         }
-         if(node.getNext() != null){
-             return delteNodeRecuresion(node.getNext(),no);
-         }else{
-             return false;
-         }
-     }
- }
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();
    }
}