【Java数据结构】---List(LinkedList)

乐观学习,乐观生活,才能不断前进啊!!!

我的主页:optimistic_chen

我的专栏:c语言Java

欢迎大家访问~
创作不易,大佬们点赞鼓励下吧~

前言

在这里插入图片描述
上篇博客详细写了ArrayList的相关问题,包括上图(极其重要),我会在最近几篇博客中都有附上。

ArrayList的优点很明显,底层逻辑是一个数组,它通过下标去访问数据的速度非常快。但是在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低

所以java集合框架中引入了LinkedList类,即链表结构。

链表(MySingleList)

链表,作为线性表的一种,它与顺序表截然相反:链表是一种物理存储结构 非连续存储结构, 数据元素的逻辑顺序是通过链表中的引用 链接次序实现的

之前C语言了解过这方面的知识C语言—链表专题,所以我们有一定的基础,我相信链表对于大家一定是熟悉的存在。我们先由易入难,先自己尝试一下单链表(MySingleList)的功能实现,为接下来的 LinkedList(无头双向链表) 打下基础.

接口

public interface IList {
    public void addFirst(int data);

    public void addLast(int data);

    public void addIndex(int index,int data);

    public boolean contains(int key);

    public void remove(int key);

    public void removeAllKey(int key);

    public int size();

    public void clear();

    public void display();
}

具体功能代码

头插法

@Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next=head;
        head=node;
    }

尾插法

@Override
    public void addLast(int data) {
        ListNode node =new ListNode(data);
        if(head==null){
            head = node;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur= cur.next;
        }
        cur.next=node;
    }

任意位置插入,第一个数据节点为0号下标

@Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||index>len){
            System.out.println("index位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        ListNode cur=head;
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        ListNode node=new ListNode(data);
        node.next= cur.next;
        cur.next=node;
    }

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

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

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

@Override
    public void remove(int key) {
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode cur=findNodeOfKey(key);
        if(cur==null){
            return;
        }
        ListNode del= cur.next;
        cur.next=del.next;

    }
    private ListNode findNodeOfKey(int key){
        ListNode cur=head;
        while(cur.next!=null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur= cur.next;
        }
        return null;
    }

删除所有值为key的节点

@Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }

        ListNode prev=head;
        ListNode cur=head.next;

        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur= cur.next;
            }else{
                prev=cur;
                cur= cur.next;
            }
            if(head.val==key){
                head=head.next;
                return;
            }
        }
    }

得到单链表的长度

@Override
    public int size() {
        int len=0;
        ListNode cur=head;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }

清空链表

@Override
    public void clear() {
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }

LinkedList简介

在这里插入图片描述

在这里插入图片描述

LinkedList的底层是双向链表结构由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。

总结:

  1. LinkedList实现了List接口
  2. LinkedList的底层使用了双向链表
  3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
  4. LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
  5. LinkedList比较适合任意位置插入的场景

LinkedList的模拟实现

在这里插入图片描述

头插法

@Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        if(head==null){
            head=last=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

尾插法

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

任意位置插入,第一个数据节点为0号下标

@Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||index>len){
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        ListNode cur=findIndex(index);
        ListNode node=new ListNode(data);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.next=node;
    }
private ListNode findIndex(int index){
        ListNode cur=head;
        while(index!=0){
            cur= cur.next;
            index--;
        }
        return cur;
    }

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

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

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

@Override
    public void remove(int key) {
        ListNode cur=head;
        while(cur!=null){
            //开始删除
            if(cur.val==key){
                //有可能是头节点
                if(cur==head){
                    head = head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }else{
                    cur.prev.next= cur.next;
                    if(cur.next==null){
                        //有可能是尾节点
                        last= last.prev;
                    }else{
                        cur.next.prev= cur.prev;
                    }
                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }

删除所有值为key的节点

@Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while(cur!=null){
            //开始删除
            if(cur.val==key){
                //有可能是头节点
                if(cur==head){
                    head = head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }else{
                    cur.prev.next= cur.next;
                    if(cur.next==null){
                        //有可能是尾节点
                        last= last.prev;
                    }else{
                        cur.next.prev= cur.prev;
                    }
                }
            }else{
                cur = cur.next;
            }
        }

    }

LinkedList的使用

前面我们自主实现了LinkedList的各种方法,只是为了大家更好的理解和学习,以后做题可能会运用到类似的想法或思路,可以更加高效的完成题目,平时也可以直接使用LinkedList下的方法。如下图:
在这里插入图片描述

LinkedList的构造

在这里插入图片描述

public static void main(String[] args) {
        List<Integer> list1=new LinkedList<Integer>();
        LinkedList<Integer> list2 = new LinkedList<Integer>();
        
   //使用ArrayList构造LinkedList     
        List<String> list3=new ArrayList<>();
        List<String> list4=new LinkedList<>(list3);
    }

为什么可以有的构造可以用List,有的用LinkedList,甚至出现了ArrayList?一切都是这张贯穿整个数据结构的图。
在这里插入图片描述

LinkedList的方法

public static void main(String[] args) {
  LinkedList<Integer> list = new LinkedList<>();
  list.add(1);  // add(): 表示尾插
  list.add(2);
  list.add(3);
  list.add(4);
  list.add(5);
  list.add(6);
  list.add(7);
  System.out.println(list.size());
  System.out.println(list);

  // subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
  List<Integer> list2 = list.subList(0, 3); 
  System.out.println(list);
  System.out.println(list2);
  list.clear();        // 将list中元素清空
  System.out.println(list.size());
}

LinkedList的遍历

之前ArrayList博客【Java数据结构】—List(ArrayList) 讲过线性表的遍历,这次补充一下…

public static void main(String[] args) {
  LinkedList<Integer> list = new LinkedList<>();
  list.add(1);  // add(): 表示尾插
  list.add(2);
  list.add(3);
  list.add(4);
  list.add(5);
  list.add(6);
  list.add(7);
  System.out.println(list.size());
  
 // 使用反向迭代器---反向遍历
  ListIterator<Integer> it = list.listIterator(list.size());
  while (it.hasPrevious()){
    System.out.print(it.previous() +" ");
 }
}

ArrayList和LinkeddList的区别

不同ArrayListLinkedList
存储空间物理上一定连续逻辑上连续,但物理上不一定连续
随机访问支持O(1)不支持O(n)
头插空间不够时扩容不存在容量的概念
运用场景元素高效存储+频繁访问频繁任意位置的插入和删除

完结

好了,到这里Java语法部分就已经结束了~
如果这个系列博客对你有帮助的话,可以点一个免费的赞并收藏起来哟~
可以点点关注,避免找不到我~ ,我的主页:optimistic_chen
我们下期不见不散~~Java

下期预告: 【Java数据结构】- - - List

评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值