双向链表的代码实现及增删改和遍历操作

158 阅读1分钟

定义HeroNode,每一个HeroNode对象都是一个结点

class HeroNode{
    public int id;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个结点,默认为空
    public HeroNode pre;//指向前一个结点,默认为空
    //构造器
    public HeroNode(int id,String name,String nickname){
        this.name=name;
        this.nickname=nickname;
        this.id=id;
    }


    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", nickname='" + nickname + ''' +
                '}';
    }
}
class DoubleLinklistDemo {

    //先初始化一个头节点,头节点不要动,不存放具体的数值
    private HeroNode head=new HeroNode(0,"","");

    //返回头节点
    public HeroNode getHead(){
        return head;
    }

    //显示链表(遍历)
    public void list(){
        //判断链表是否为空
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        //因为头节点不动,因此我们需要一个辅助变量来遍历
        HeroNode temp=head.next;
        while(true){
            //判断是否到链表最后
            if (temp==null){
                break;
            }
            //输出节点的信息
            System.out.println(temp);
            //将temp后移
            temp= temp.next;
        }
    }

  //增添数据操作
    public void add(HeroNode herNode){
        //因为head节点不能动,因此我们需要一个辅助遍历temp
        HeroNode temp=head;
        //遍历链表,找到最后
        while(true){
            if (temp.next == null) {
                break;
            }
            //如果没有找到最后,将temp后移
            temp=temp.next;
        }
        //当退出while循环时,temp就指向了链表的最后
        //形成一个双向链表
        temp.next=herNode;
        herNode.pre=temp;
    }

  //修改数据操作
    public void update(HeroNode newHeroNode){
        //判断是否为空
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        //找到需要修改的节点,根据id编号
        //定义一个辅助变量
        HeroNode temp=head;
        boolean flag=false;
        while(true){
            if (temp==null){
                break;
            }
            if (temp.id== newHeroNode.id){
                //找到
                flag=true;
                break;
            }
            temp=temp.next;
        }
        //根据flag判断是否找到需要修改的节点
        if(flag){
            temp.name= newHeroNode.name;
            temp.nickname= newHeroNode.nickname;
        }else{
            System.out.println("未找到节点,不能修改");
        }
    }

    public void delete(int id){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=head.next;
        boolean flag=false;
        while(true){
            if (temp==null){
                break;
            }
            if (temp.id == id){
                //找到待删除节点的第一个节点temp
                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("要删除的节点不存在");
        }
    }
}