无脑入单向无头链表的实现| ArrayList和LinkedList的区别

本文分析了ArrayList在插入和删除操作中的性能问题,介绍了LinkedList(链表)的多种结构,重点讲解了无头单向非循环链表的实现,包括头插法、尾插法、索引插入、查找与删除功能,以及单向LinkedList类的详细代码示例。
摘要由CSDN通过智能技术生成

1. ArrayList的缺陷

上节课已经熟悉了ArrayList的使用,并且进行了简单模拟实现。通过源码知道,ArrayList底层使用数组来存储元素。

由于其底层是一段连续空间,当 ArrayList 任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后 搬移,时间复杂度为 O(n) ,效率比较低,因此 ArrayList 不适合做任意位置插入和删除比较多的场景 。因此: java集合中又引入了LinkedList ,即链表结构。

2. 链表

2.1 链表的概念及结构

链表也是线性表的一种,链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:  

1. 单向或者双向

2. 带头或者不带头 

3. 循环或者非循环 

虽然有这么多的链表的结构,但是我们重点掌握两种: 

  1. 无头单向非循环链表结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
  2. 无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

2.2 无头单向非循环链表实现

2.2.1 链表的类结构

2.2.2 头插法

2.2.3尾插法

2.2.4在index前面插入

2.2.5查找是否包含关键字key是否在单链表当中

 

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

2.2.7删除所有出现关键字为key的节点

 2.2.8得到单链表的长度

2.2.9释放链表

2.2.10节点打印 

2.2.11得到节点长度

 

3.单向无头链表实现代码 

MyLinkedList类

import java.util.Arrays;

public class SingleLinkedList {

    static class ListNode {
        public int val;
        public ListNode next;

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

    //记录头节点
    public ListNode head;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
//        //更为简单的方式
//        node.next = head;
//        head = node;
        if (head == null) {
            head = node;
        } else {
            node.next = head;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //如果链表为空,直接插入
        if (head == null) {
            head = node;
        } else {
            //如果链表不为空
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //任意位置前插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        //1.判断index的合法性
        try {
            checkIndex(index);
        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }
        //第一个节点
        if (index == 0) {
            //直接头插
            addFirst(data);
        } else if (index == size()) {
            //尾节点,直接尾插
            addLast(data);
        } else {
            //插入中间节点
            ListNode newNode = new ListNode(data);
            ListNode listNode = findIndexSubOne(index);
            newNode.next = listNode.next;
            listNode.next = newNode;
        }

    }

    //判断index的合法性
    private void checkIndex(int index) throws IndexNotLegalException {
        if (index < 0 || index > size()) {
            throw new IndexNotLegalException("index不合法");
        }
    }

    //查找index的前一个节点
    private ListNode findIndexSubOne(int index) {
        int count = 0;
        ListNode cur = head;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null){
            System.out.println("没有节点,删除失败!");
            return;
        }
        ListNode cur = head;
        //头结点是key
        if (head.val == key) {
            head = head.next;
            System.out.println("删除成功!");
            return;
        }
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                System.out.println("删除成功!");
                return;
            }
            cur = cur.next;
        }

        System.out.println("删除失败!没有此节点");
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        //1. 判空
        if(this.head == null) {
            return;
        }
        //2. 定义prev 和 cur
        ListNode prev = head;
        ListNode cur = head.next;
        //3.开始判断并且删除
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            }else {
                prev = cur;//prev = prev.next;
            }
            cur = cur.next;
        }
        //4.处理头节点
        if(head.val == key) {
            head = head.next;
        }
        System.out.println("删除成功!");

    }

    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //释放链表
    public void clear() {
//        //直接释放
//        head = null;
        //安全释放
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            //cur.val = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
    //节点打印
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}

Test测试类

public class Test {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //头插法
//        singleLinkedList.addFirst(1);
//        singleLinkedList.addFirst(2);
//        singleLinkedList.addFirst(3);
//        //尾插法测试
//        singleLinkedList.addLast(1);
//        singleLinkedList.addLast(1);
//        singleLinkedList.addLast(1);

        singleLinkedList.addLast(3);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(5);

//        //任意位置插入测试
//        singleLinkedList.addIndex(0,5);
//        //是否含有此元素测试
//        System.out.println(singleLinkedList.contains(3));
//        singleLinkedList.remove(3);
        singleLinkedList.removeAllKey(3);
        singleLinkedList.display();
        System.out.println(singleLinkedList.size());

    }
}

IndexNotLegalException运行时异常类

public class IndexNotLegalException extends RuntimeException{
    //无参构造方法
    public IndexNotLegalException() {
    }
    //含一个参数参构造方法
    public IndexNotLegalException(String message) {
        super(message);
    }
}


好啦,今天就到这里了,我们下期再见,感谢观看。

  • 97
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 98
    评论
评论 98
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

再无B~U~G

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

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

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

打赏作者

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

抵扣说明:

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

余额充值