Java实现双向链表

  首先介绍一下双向链表,他是数据结构中的一种,跟单链表一样,既可有头节点,也可无头节点

Java实现双向链表_第1张图片

下面我们用Java实现双向链表

1.首先抽象出来一个节点类

//定义节点类
class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;       //指向下一个节点,默认为null
    public HeroNode2 pre;        //指向上一个节点,默认为null

    //构造器

    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 + '\'' +
                '}';
    }
}

2.然后是双链表核心的功能实现(增删改)

class DoubleLinkedList{
    //初始化头节点
    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;
        }
    }

    /**
     * 双向链表节点的添加
     *
     * @param heroNode
     */
    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 addByOrder(HeroNode2 heroNode){
        HeroNode2 temp = head;
        boolean flag = false;
        while(true){
            if(temp.next == null){
                break;
            }
            if(temp.next.no == heroNode.no){
                flag = true;
                break;
            }
            if(temp.next.no > heroNode.no){
                break;
            }
            temp = temp.next;
        }

        if(flag){
            System.out.println("要添加的节点编号已经存在");
        }else{
            if (temp.next != null) {
                temp.next.pre = heroNode;
                heroNode.next = temp.next;
            }
            temp.next = heroNode;
            heroNode.pre = temp;
            }
        }




    /**
     * 双向链表节点的修改
     *
     * @param newHeroNode
     */
    public void update(HeroNode2 newHeroNode){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(true){
            if(temp == null){
                break;
            }
            if(temp.no == newHeroNode.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        }else {
            System.out.println("没有找到编号为" + newHeroNode.no + "的节点");
        }
    }

    /**
     * 双向链表中节点的删除
     *
     * @param no
     */
    public void delete(int no){

        //这里 与单链表不同的是,直接从head后第一个节点开始找就可以,辅助指针直接指向第一个有效节点
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(true){
            if(temp.next == null){
                break;
            }
            if(temp.next.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("要删除的编号为" + no + "节点不存在");
        }
    }


}


3.Demo类创建结点添加到双链表中,进行功能测试

public class DoubleLinkedListDemo {

    public static void main(String[] args) {
        HeroNode2 hero1 = new HeroNode2(1,"宋江","及时雨");
        HeroNode2 hero2 = new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3,"吴用","智多星");
        HeroNode2 hero4 = new HeroNode2(4,"林冲","豹子头");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.list();
        System.out.println("---------------------------------------------------------");
        //修改
        HeroNode2 newHeroNode = new HeroNode2(4,"冲冲","豹富");
        doubleLinkedList.update(newHeroNode);
        doubleLinkedList.list();
        System.out.println("---------------------------------------------------------");

        //删除
        doubleLinkedList.delete(3);
        doubleLinkedList.list();
        System.out.println("---------------------------------------------------------");

        //按照顺序添加
        HeroNode2 newHeroNode2 = new HeroNode2(6,"科科","小马");
        HeroNode2 newHeroNode3 = new HeroNode2(5,"菲菲","小羊");
        doubleLinkedList.addByOrder(newHeroNode2);
        doubleLinkedList.addByOrder(newHeroNode3);
        doubleLinkedList.list();
        System.out.println("---------------------------------------------------------");

    }

}

你可能感兴趣的:(数据结构与算法,java,链表,数据结构)