快速入门无头双链表

无头双向单链表的基础功能实现

在单链表的学习中,我们已经掌握了求单链表的长度,是否包含了某一个元素,以及打印单链表,无头双链表的求长度,判断是否包含了某一个元素,打印双链表,都是与单链表的写法一样的,所以这里就不在多做赘述。

头插法

变化前
在这里插入图片描述

变化后
在这里插入图片描述
代码实现:

 //头插法
        public void addFirst(int data){
        listnode add=new listnode(data);
        if (head==null){//还得考虑链表为空的情况
            head=add;
            last=add;
            return;
        }
        add.next=head;
        head.prve=add;
        head=add;
        }

尾插法

变化前
在这里插入图片描述

变化后
在这里插入图片描述
代码实现:

 //尾插法
        public void addLast(int data){
            listnode add=new listnode(data);
        if (head==null){//同样需要考虑要是有空列表的情况
            head=add;
            last=add;
            return;
        }
        last.next=add;
        add.prve=last;
        last=add;
        }

删除第一次出现关键字为key的节点

一般情况
在这里插入图片描述
出现在头部情况(假设要删除的第一次数据在头部出现):
在这里插入图片描述
出现在尾部(假设要删除的数据最后才第一次出现):
在这里插入图片描述
对以上情况进行分析之后,我们必须通过遍历来解决,具体实现代码如下:

//删除第一次出现关键字为key的节点
        public void remove(int key){
            if (head==null)return;
        listnode cur=head;
        while (cur!=null){
            if (cur.val==key){
                if (cur==head){
                    cur.next.prve=null;
                    head=head.next;
                }else{
                    cur.prve.next=cur.next;
                    if (cur==last){
                        last=cur.prve;
                    }else{
                        cur.next.prve=cur.prve;
                    }
                }
                return;
            }else{
                cur=cur.next;

            }
        }
        }

但是在这里代码是有缺陷的,因为如果就是一个节点的时候,那么此时代码会报错,我们就需要单独讨论一下,具体代码实现如下:

        public void remove(int key){
            if (head==null)return;
        listnode cur=head;
        while (cur!=null){
            if (cur.val==key){
                if (cur==head){
                    head=head.next;
                    if (head!=null){
                        head.prve=null;
                    }else{
                        last=null;//尾节点要变成null,不然会被一直引用
                    }
                }else{
                    cur.prve.next=cur.next;
                    if (cur==last){
                        last=cur.prve;
                    }else{
                        cur.next.prve=cur.prve;
                    }
                }

            }else{
                cur=cur.next;

            }
        }
        }

删除所有的值为key的节点

这个就需要在删第一次为key的基础上,把return去掉,然后要让cur一直往下走,具体实现代码如下:

//删除所有值为key的节点
        public void removeAllKey(int key){
            if (head==null)return;
            listnode cur=head;
            while (cur!=null){
                if (cur.val==key){
                    if (cur==head){
                        head=head.next;
                        if (head!=null){
                            head.prve=null;
                        }else{
                            last=null;
                        }

                    }else{
                        cur.prve.next=cur.next;
                        if (cur.next!=null){
                            cur.next.prve=cur.prve;
                        }else{
                           last=last.prve;
                        }
                    }
                }
                    cur=cur.next;
            }
        }

任意位置插入

插入前:
在这里插入图片描述
插入后
在这里插入图片描述

代码实现如下:

 //任意位置插入,第一个数据节点为0号下标
        public listnode search(int index){
            if (index<0||head==null)return null;
            listnode cur=head;
            while (index!=0){
                cur=cur.next;
                if (cur==null){//说明超出链表的长度
                    return null;
                }
                index--;
            }
            return cur;
        }
        public void addIndex(int index,int data){
            listnode listnode=new listnode(data);
            if (index==0){//如果是第一个位置就是头插法
                addFirst(data);
                return;
            }
            if (index==size()){//最后一个位置就是尾插法
                addLast(data);
                return;
            }
            listnode ret=search(index);
            if (ret==null)return;
            ret.prve.next=listnode;
            listnode.next=ret;
            listnode.prve=ret.prve;
            ret.prve=listnode;
        }

清空双链表

在这里插入图片描述
代码实现:

       public void clear(){
        while(head!=null){
            listnode cur=head.next;
            head.prve=null;
            head.next=null;
            head=cur;
        }
        last=null;//要把尾节点手动置为null,不然还在被引用
        }

双链表基本功能实现源码
单链表的功能实现

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

to Keep

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值