数据结构-双向链表

5 篇文章 0 订阅

定义

1.定义:双向链表(双链表)是链表的一种。和单链表一样,双链表也是由节点组成,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。

图解

2.图解·:
在这里插入图片描述

单链表的缺点:

  1. 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
  2. 单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除,所以前面我们单链表删除
    时节点,总是找到 temp,temp 是待删除节点的前一个节点。

遍历

1.单链表一样,只是可以向前,也可以向后查找

添加 (默认添加到双向链表的最后)

(1) 先找到双向链表的最后这个节点
(2) temp.next = newHeroNode
(3) newHeroNode.pre = temp;

修改

思路和 原来的单向链表一样.

删除

(1) 因为是双向链表,因此,我们可以实现自我删除某个节点
(2) 直接找到要删除的这个节点,比如 temp
(3) temp.pre.next = temp.next
(4) temp.next.pre = temp.pr
在这里插入图片描述

指定位置插入

(1).找到指定位置的节点
newNode.nextNode=temp.nextNode;
temp.nextNode.preNode = newNode;
newNode.preNode=temp;
temp.nextNode=newNode;
在这里插入图片描述

Node类

public class HeroNode {
    public  int no;
    public String name;
    public HeroNode preNode;
    public HeroNode nextNode;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;

    }

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

DoubleLinkedList类

public class DoubleLinkedList {

    //遍历
    public void printList(HeroNode head) {
        HeroNode temp=null;
        if (!isEmpty(head)) {
            temp=head.nextNode;
            while(temp!=null){
                System.out.println(temp.toString());
                temp=temp.nextNode;
            }
        }else{
            throw new myException("链表为空!");
        }
    }
    //添加一个节点
    public void addNode(HeroNode head,HeroNode newNode){
        if(isEmpty(head)){
            head.nextNode=newNode;
            newNode.preNode=head;
        }else {
            HeroNode temp = head.nextNode;
            while (temp.nextNode != null) {
                temp = temp.nextNode;
            }

            temp.nextNode=newNode;
            newNode.preNode=temp;
        }
    }
    //修改一个节点
    public void modifyNode(HeroNode head,HeroNode newNOde){
        HeroNode temp=null;
        boolean flag=true;
        if (!isEmpty(head)) {
            temp=head.nextNode;
            while (flag){
                if(temp.no==newNOde.no){
                    flag=false;
                }else if(temp.nextNode==null){
                    throw new myException("未找到");

                }
                else{
                    temp=temp.nextNode;
                }


            }

            newNOde.preNode=temp.preNode;
            newNOde.nextNode=temp.nextNode;
            temp.preNode.nextNode=newNOde;
            if(temp.nextNode!=null) {
                temp.nextNode.preNode = newNOde;
            }
            System.out.println("修改成功");

        }else{
            throw new myException("链表为空!");
        }
    }
    //删除一个节点
    public void removeNode(HeroNode head,int no){
        HeroNode temp=null;
        boolean flag=true;
        if (!isEmpty(head)) {
            temp=head.nextNode;
            while (flag){
                if(temp.no==no){
                    flag=false;
                }else if(temp.nextNode==null){
                    throw new myException("未找到");
                }
                else{
                    temp=temp.nextNode;
                }

            }
            temp.preNode.nextNode=temp.nextNode;
            //判断是否为最后一个节点
            if(temp.nextNode!=null){
                temp.nextNode.preNode=temp.preNode;
            }

            System.out.println("修改成功");
        }else{
            throw new myException("链表为空!");
        }
    }
    public void Insert(HeroNode head, int index,HeroNode newNode)
    {
        if(index<=0||index>getLenght(head)){
            throw  new myException("索引不符合规格");
        }
        HeroNode temp=head;
        for(int i=0;i<index;i++){
            temp=temp.nextNode;
        }
        newNode.nextNode=temp.nextNode;
        //判断是否最后一个节点
        if(temp.nextNode!=null) {
            temp.nextNode.preNode = newNode;
        }
        newNode.preNode=temp;
        temp.nextNode=newNode;

    }
    //是否为空
    public boolean isEmpty(HeroNode head){
        boolean flag=true;
        if (head.nextNode!=null) {
            flag=false;
        }
            return flag;
        }
    //长度
    public int getLenght(HeroNode head){
        HeroNode temp=null;
        int lenght=0;
        if(!isEmpty(head)){
            temp=head;
            while (temp.nextNode!=null){
                temp=temp.nextNode;
                lenght++;
            }
        }
        return lenght;
    }



}

class  myException extends RuntimeException{
    public myException(String e){
        super(e);
    }
}

测试类

public class DoubleLinkedListTest {
    @Test
    public void testLinkedList(){
        DoubleLinkedList test=new DoubleLinkedList();
        HeroNode head=new HeroNode(0, "");
        HeroNode t1=new HeroNode(1, "美国队长");
        HeroNode t2=new HeroNode(2, "雷神");
        HeroNode t3=new HeroNode(3, "浩克");
        //判断空
        System.out.println(test.isEmpty(head));
        //添加
        test.addNode(head,t1);
        test.addNode(head,t2);
        test.addNode(head,t3);
        //输出
        test.printList(head);
        //长度
        System.out.println(test.getLenght(head));
        //修改
        test.modifyNode(head,new HeroNode(2,"蚁人"));
        test.printList(head);
        //test.modifyNode(head,new HeroNode(4,"蚁人"));//抛出异常
        //删除
        test.removeNode(head,3);
        test.printList(head);
        //test.removeNode(head,5);//抛出异常
        //插入
        test.Insert(head,1,new HeroNode(4,"金刚狼"));
        test.printList(head);

    }

}

结果

true
HeroNode{no=1, name='美国队长}
HeroNode{no=2, name='雷神}
HeroNode{no=3, name='浩克}
3
修改成功
HeroNode{no=1, name='美国队长}
HeroNode{no=2, name='蚁人}
HeroNode{no=3, name='浩克}
修改成功
HeroNode{no=1, name='美国队长}
HeroNode{no=2, name='蚁人}
HeroNode{no=1, name='美国队长}
HeroNode{no=4, name='金刚狼}
HeroNode{no=2, name='蚁人}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霓乤

谢谢支持,菜鸟会继续努力..

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值