【数据结构】Java实现一个双向不带头节点的非循环链表的增删查改

我们都知道链表分为单链表和双向链表,今天我们就来实现一个双向链表

首先我们先创建一个节点

    class ListNode{
        int val;
        ListNode next;
        ListNode prev;

        ListNode(int val){
            this.val = val;
        }
    }

我们创建好链表的节点以后再来创建一个双向链表

    ListNode head;//定义一个头指针
    ListNode tail;//定义一个尾指针

    public void creatList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        //将链表向后连接起来
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        //将链表向前连接起来
        node5.prev = node4;
        node4.prev = node3;
        node3.prev = node2;
        node2.prev = node1;

        this.head = node1;
        this.tail = node5;
    }

通过地址将前后连接起来

同时定义了两个指针,一个指向第一个节点,一个指向了最后一个节点

然后我们实现一下最简单的功能,返回链表长度

这里的操作与单链表是相同的

//将链表的长度打印出来
    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

同时我们再来实现将链表进行打印的功能

    //将链表进行打印
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

将简单的功能实现完成以后我们实现添加元素的功能

首先介绍头插法

实现功能的时候要注意判断链表是否为空,如果是空链表那么单独操作了

    public void addFirst(int value){
        ListNode node = new ListNode(value);//创建一个节点
        if (head == null){
            this.head = node;
            this.tail = node;   //如果为空链表,创建一个节点,并将头尾指针指向这个节点
        }
        else{
            node.next = head;
            head.prev = node;

            this.head = node;
            this.tail = node.next;//将两个节点链接完成以后,将头指针指向第一个节点
        }
    }

头插法完成以后我们实现以下尾插法

同样也是要注意链表里面的节点

如果是空链表的情况是怎样的

如果是只有一个节点的情况是怎样的

    public void addLast(int value){
        ListNode node = new ListNode(value);

        //如果是一个空链表
        if (head == null){
            this.head = node;
            this.tail = node;
        }

        //如果链表只有一个节点
        if (head.next == null){
            head.next = node;
            this.tail = node;
        }

        //如果有多个节点的情况下
        tail.next = node;
        node.prev = tail;
        this.tail = node;
    }

最后我们实现在任意下标进行添加元素

首先我们要先找到前一个下标方便我们对节点进行修改

    //找到下标前一个元素的位置
    public ListNode findPreIndex(int index){
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            if (count == index-1){
                return cur;
            }
            count++;
            cur = cur.next;
        }
        return null;
    }

然后开始我们的修改链表的链接

    public void indexAdd(int index,int value){
        //下标有误的处理
        if (index < 0 || index >= size()){
            throw new RuntimeException("下标有误!请检查");
        }

        //如果在第一个下标插入那么就使用头插法
        if (index == 0){
            addFirst(value);
        }

        //如果在最后一个下标插入那么就使用尾插法
        if (index == size()){
            addLast(value);
        }

        //在任意一个下标插入
        ListNode nodePrev = findPreIndex(index);
        ListNode node = new ListNode(value);

        node.next = nodePrev.next;
        nodePrev.next.prev = node;
        nodePrev.next = node;
        node.prev = nodePrev;
    }

我们在实现完成增加元素以后来完成一个简单的查找是否包含元素的操作

    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

最后我们讲以下如何进行删除元素

我们首先实现删除第一次出现的某一个元素

我这里的思路使用大量的if-else语句,如果有更好的思路可以在评论区提出

    //删除第一次出现的某一个元素
    public void remove(int value){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == value) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        this.tail = null;
                    } else {
                        head.prev = null; //没有人指向他了那么就被删除了
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;
                    }
                }
                return; //这个return的目的就是删除完成第一次出现的元素以后,退出循环
            }
            cur = cur.next;
        }
    }

下面我来讲如果实现删除所有指定的元素

内容同上面的大体相同

只不过缺少一个终止循环的语句从而使得可以一直删除

    public void removeAllKey(int value) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == value) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        this.tail = null;
                    } else {
                        head.prev = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next; //这里没有return说明可以一直查找,直到全部删除完
        }
    }

最后我们讲如何删除所有元素

首先我们要知道,在链表里面删除一个元素的定义是

如果一个节点没有被任何一个节点指向,那么这个节点就被删除了

所以在双向链表里面想要删除所有元素就必须一个节点一个节点的删除

不能删除头节点和尾巴节点

所以就有了以下操作

    //删除所有元素
    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值