package datastructure;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        HeroNode2 heroNode21 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode22 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode23 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode24 = new HeroNode2(4, "林冲", "豹子头");
        HeroNode2 heroNode25 = new HeroNode2(4, "冲", "头");
        HeroNode2 heroNode26 = new HeroNode2(5, "林冲", "豹子头");
        doubleLinkedList.add(heroNode21);
        doubleLinkedList.add(heroNode24);
        doubleLinkedList.add(heroNode23);
        doubleLinkedList.add(heroNode22);
        doubleLinkedList.list();
        System.out.println();
        doubleLinkedList.del(2);
        doubleLinkedList.del(1);
        doubleLinkedList.list();
        doubleLinkedList.update(heroNode25);
        System.out.println();
        doubleLinkedList.list();
        doubleLinkedList.update(heroNode26);
        System.out.println();
        doubleLinkedList.list();
    }
}

class DoubleLinkedList{
    // 遍历双向链表的方法
    // 显示链表[遍历]
    private HeroNode2 head = new HeroNode2(0,"","");
    public void list(){
        // 判断链表是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while (true){
            if (temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public void add(HeroNode2 heroNode2){
        HeroNode2 temp = head;
        while(true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode2;
        heroNode2.pre = temp;
    }
    public void update(HeroNode2 heroNode2){
        if (head == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag =false;
        while (true){
            if (temp == null){
                break;
            }
            if (temp.no == heroNode2.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name = heroNode2.name;
            temp.nickname = heroNode2.nickname;
        }else {
            System.out.printf("编号%d的节点不存在",heroNode2.no);
        }
    }
    public void del(int no){
        if (head.next == null){
            System.out.println("链表为空,无法删除");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if (temp == null){
                break;
            }
            if (temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.pre.next = temp.next;
            if (temp.next != null){
                temp.next.pre = temp.pre;
            }
        }else {
            System.out.printf("要删除的节点%d不存在\n",no);
        }

    }
}

class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}