双向链表之于单向....plus:区别


## //创造节点,比较与单向的不同

public class HeroNode {
    public int num;
    public String name;
    public String Nickname;
    public HeroNode next;
    public HeroNode pre;  //多一个pre节点

    public HeroNode(int num, String name, String nickname) {
        this.num = num;
        this.name = name;
        Nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", Nickname='" + Nickname + '\'' +
                '}';
    }

}

双向链表类

public class DoubleLinkedList {
    private HeroNode head=new HeroNode(0, "","");   //一定要注意对链表头的初始化,不然会出现空指针的异常
    public static int count;
    
//    public void delete(HeroNode node)   //第一种删除方法         试探法
//    {
//        HeroNode temp=head;
//        while (true)
//        {
//            if (temp.next==null)
//            {
//                System.out.println("no such person!");
//                break;
//            }
//            if (temp.next.num==node.num)   这里需要试探下一个节点,来确定目标节点的前一个
//            {
//                temp.next=temp.next.next;
//                temp.next.next.pre=temp;
//                count--;
//                break;
//            }
//            temp=temp.next;
//        }
//
//    }
    public void delete(HeroNode node)    //第二种删除方法,得益于双向特性
    {
        HeroNode temp=head;
        while (true)
        {
            if (temp.next==null)  //删除最后一个节点时需要另外考虑,会有空指针异常或无法删除最后一个节点情况出现(取决于循环的控制条件)
            {
                if (temp.num==node.num)
                {
                    temp.pre.next=null;
                    break;   //注意找到后需要退出循环,否则会进入下面的删除操作造成空指针异常

                }else {
                    System.out.println("no such person!");
                    break;
                }
            }
            if (temp.num==node.num)
            {
                temp.pre.next=temp.next;   //这里不需要在目标节点的前一个节点暂停,因为目标节点的pre就可找到前一个节点
                temp.next.pre=temp.pre;
                break;
            }else {
                temp=temp.next;
            }
        }
    }
    public void update(HeroNode node)   //完全等同于单向链表
    {
        HeroNode temp=head;
        while (true)
        {
            if (temp.next==null) {
                System.out.println("don't exit!");
                break;
            }
            if (temp.next.num == node.num) {
                temp.next.name=node.name;
                temp.next.Nickname=node.Nickname;
                break;
            }
            temp=temp.next;

        }

    }
    public void addByNumber(HeroNode someone)   //仅需加上pre的指向
    {
        HeroNode temp=head;
        while (true)
        {
            if (temp.next==null)
            {
                temp.next=someone;
                someone.pre=temp;
                count++;
                break;
            }
            if (temp.next.num>someone.num)
            {
                someone.next=temp.next;
                temp.next=someone;
                someone.pre=temp;
                someone.next.pre=someone;
                count++;
                break;
            }else if (temp.next.num==someone.num)
            {
                System.out.println("exist the same number,so the adding fails");
                break;
            }else{
                temp=temp.next;
            }

        }

    }
    public void  showing()
    {
        HeroNode temp=head.next;
        while (temp.next!=null)
        {
            System.out.println(temp);
            temp=temp.next;
        }
        System.out.println(temp);

    }
    public void Reshowing()
    {
        HeroNode temp=head.next;
        while (temp.next!=null)
        {
            temp=temp.next;
        }
        while (temp.pre!=null)
        {
            System.out.println(temp);
            temp=temp.pre;
        }
    }

}

plus:单向链表 : 单向链表代码实现

双单区别
1.单向链表 具有试探性(即每次都是定位到目标节点位置的前一个,然后实现删除或添加)
2.双向链表 更为直接 只需直接定位到目标节点,然后利用pre找到前一个,next找到后一个,但是需要建立更多的联系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值