双向链表详解

目录

一,双向链表的概念及结构 

二,双向链表的方法及其实现

2.1 双向链表

2.2 addFirst(int data) - 头插法 

2.3 addLast(int data) - 尾插法

2.4 size() - 链表长度

2.5 display() - 打印链表内容

2.6 clear() - 删除链表

2.7 addIndex(int index, int data) - 任意位置插入

2.8 contains(int key) - 链表当中是否有key

2.9 remove(int key) - 删除链表中第一次出现的key

2.10 removeAllKey(int key) - 删除所有值为key的节点


一,双向链表的概念及结构 

与单向链表相同,只不过每一个节点中多了一个空间来存储上一个节点的地址,结构如下图所示:

二,双向链表的方法及其实现

2.1 双向链表

链表类的定义及我们接下来要实现的方法,如下代码:

public class LinkedList {
    static class ListNode{//节点类 - 因为节点是链表的属性,所以用static来修饰
        //节点的成员
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;//头节点 - 不是每个节点都是头节点,它是链表的属性,所以定义在节点类外面
    public ListNode last;//尾节点 - 同头节点

    //头插法
    public void addFirst(int data){}

    //尾插法
    public void addLast(int data){}

    //在任意位置插入,假设第一个节点的下标为0
    public void addIndex(int index, int data){}

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){}

    //删除第一次出现关键字为key的节点
    public void remove(int key){}

    //删除所有值为key的节点
    public void removeAllKey(int key){}

    //得到单链表的长度
    public int size(){}

    //打印链表内容
    public void display(){}

    //删除链表
    public void clear(){}
}

2.2 addFirst(int data) - 头插法 

顾名思义,就是在头节点之前插入一个节点,并将其变成头节点。 如图

但是我们还要注意如果链表当中没有元素的情况,如图:

    public void addFirst(int data){
        ListNode node = new ListNode(data);//创建一个节点
        if(head == null){//无元素的情况
            head = node;
            last = node;
            return;
        }
        head.prev = node;//1
        node.next = head;//2
        head = node;//4
    }

2.3 addLast(int data) - 尾插法

 但是我们还要注意如果链表当中没有元素的情况,如图:

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

2.4 size() - 链表长度

遍历链表

    public int size(){
        ListNode cur = head;//不能直接用head遍历,会改变head节点
        int count = 0;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

2.5 display() - 打印链表内容

遍历链表

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();//换行
    }

2.6 clear() - 删除链表

遍历链表 - 将所有值置为空

    public void clear(){
        ListNode cur = head;        
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }

2.7 addIndex(int index, int data) - 任意位置插入

1.  要先判断他输入的下标合不合法

2.  看下标是不是头节点,尾节点,是的话直接调用头插法,尾插法

3.  如图

   public void addIndex(int index,int data){
        if(index < 0 || index > size()) {
            System.out.println("index不合法!");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        //cur拿到了index下标的节点的地址
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        node.prev = cur.prev;
        node.next = cur;
        cur.prev.next = node;
        cur.prev = node;
    }

2.8 contains(int key) - 链表当中是否有key

遍历链表

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

2.9 remove(int key) - 删除链表中第一次出现的key

1. 先找到key所在的位置

2. 判断是不是头节点,如果是且不只有一个元素 

如果是头节点且只有一个头节点:

3. 再判断是不是尾节点,如果是:

4.是中间节点,如图:

 

    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head == null){//只有一个元素
                        last = null;
                    }else {//有多个元素
                        head.prev = null;
                    }
                }else {
                    if(cur == last){//尾节点
                        last = last.prev;
                        last.next = null;
                    }else {//中间节点
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

2.10 removeAllKey(int key) - 删除所有值为key的节点

与remove方法相同,只不过删除成功后不要return,继续往后遍历:

    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head == null){
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else {
                    if(cur == last){//尾节点
                        last = last.prev;
                        last.next = null;
                    }else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                //return这里不同
            }
            cur = cur.next;
        }
    }

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一叶祇秋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值