双链表

思路如下:
在这里插入图片描述
话不多说,直接上代码~

package datastructres.linkedlist;

/**
 * @author :Yan Guang
 * @date :Created in 2021/1/9 10:21
 * @description:
 */
public class DoubleLinkListDemo {
    public static void main(String[] args) {
        System.out.println("双向链表的测试~");
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");

        DoubleLinkList list = new DoubleLinkList();
        list.add(hero1);
        list.add(hero2);
        list.add(hero3);
        list.add(hero4);
        list.list();
        System.out.println("通过指定位置加入~");

//        list.addByOrder(hero2);
//        list.addByOrder(hero1);
//        list.addByOrder(hero3);
//        list.addByOrder(hero4);
//        list.list();

        System.out.println("删除一个~");
        list.delete(1);
        list.list();
    }
}

class DoubleLinkList {
    //一个双链表也只有一个头结点,需要定义在这里~
    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    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  addByOrder(HeroNode2 heroNode){
        HeroNode2 temp = head;
        while (temp != null){
            if (temp.no == heroNode.no){
                System.out.printf("您的编号%d已经添加过了",heroNode.no);
                return;
            }
            if(temp.next == null || temp.next.no > heroNode.no){
                heroNode.pre = temp;
                heroNode.next = temp.next;
//这里要主义的地方就是如果是判断头结点的时候,需要这条指令就会
//报空指针异常,因为next是null所以没有pre,所以我们要当有一个
 //节点的时候再加上这条语句,兄弟们~
                if (temp.next!=null) {
                    temp.next.pre = heroNode;
                }
                temp.next = heroNode;
                return;
            }
            temp = temp.next;
        }
    }
    public void add(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    public void delete(int no) {
        if (head.next==null){
            System.out.println("链表为空~");
        }
        HeroNode2 temp = head.next;//这里一开始也还是把temp定义在头结点的后面一个结点
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            } else 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.println("不存在该编号,无法删除~");
        }
    }
}

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 "heroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' + "}";
    }
}

欢迎大家在评论区讨论~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值