当前位置: 首页 > news >正文

Java数据结构——LinkedList

目录

一、链表

1.1链表的概念及结构

1.2 无头双向循环链表的实现

二、LinkedList的使用

2.1 LinkedList的构造方法

2.2 LInkedList的遍历


        我们之前已经了解了ArrayList的使用,也已知它的底层是使用数组来储存元素。那么当我们使用ArrayList并且插入元素或者删除元素时,我们都需要移动插入或删除位置的后续元素,时间复杂度为O(n),将耗费大量时间。由此我们得知,如果我们需要对数据频繁插入删除,ArrayList则不适用这种场景。所以我们今天来学习LinkedList,链表结构。

一、链表

1.1链表的概念及结构

LinkedList在物理存储结构上是非连续的存储结构,我们使用引用链接次序实现连续的逻辑顺序。

从上图可以看出,链表结构在逻辑上是连续的,但是在物理上不一定连续

根据链表的不同特质,可以分成8种不同的链表结构

1.单向或者双向

2.带头或者不带头

3.循环或者不循环

8种链表结构,我们重点掌握两种:

无头单向非循环链表:结构简单,更多作为其他数据结构的子结构,在笔试面试中出现很多

无头双向循环链表:在Java中LinkedList底层实现的就是无头双向循环链表

1.2 无头双向循环链表的实现

public class LinkedList {static class ListNode{int val;ListNode next;ListNode prev;public ListNode(int val){this.val=val;}}ListNode head;ListNode last;//头插法public void addFirst(int data){ListNode node=new ListNode(data);if(head==null){head=last=node;return;}node.next=head;head=node;}//尾插法public void addLast(int data){ListNode node=new ListNode(data);if(head==null){head=last=node;return;}node.prev=last;last.next=node;last=node;}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data){if(index<0||index>size()){return;}if(index==0){addFirst(data);}else if(index==size()-1){addLast(data);}else{ListNode node=new ListNode(data);ListNode cur=head;int count=0;while(count!=index){count++;cur=cur.next;}node.next=cur;node.prev=cur.prev;node.prev.next=node;cur.prev=node;}}//查找是否包含关键字key是否在单链表当中public boolean contains(int key){ListNode cur=head;while(cur!=null){if(cur.val==key){return true;}}return false;}//删除第一次出现关键字为key的节点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{last=null;}}else{if(cur==last){last=cur.prev;cur.prev.next=cur.next;}else{cur.next.prev=cur.prev;cur.prev.next=cur.next;}}return;}else{cur=cur.next;}}}//删除所有值为key的节点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{last=null;}}else{if(cur==last){last=cur.prev;cur.prev.next=cur.next;}else{cur.next.prev=cur.prev;cur.prev.next=cur.next;}}}else{cur=cur.next;}}}//得到单链表的长度public int size(){int count=0;ListNode cur=head;while(cur!=null){count++;cur=cur.next;}return count;}//打印链表public void display(){ListNode cur=head;while(cur!=null) {System.out.println(cur.val + " ");cur = cur.next;}}//清除链表public void clear(){ListNode cur=head;ListNode curN=head.next;while(cur!=null){cur.next=cur.prev=null;cur=curN;curN=curN.next;}head=last=null;}
}

二、LinkedList的使用

2.1 LinkedList的构造方法

方法解释
LinkedList( )无参构造
public LinkedList(Collection<? extends E> c)使用其他集合容器中元素构造List
public class Test {public static void main(String[] args) {//无参构造LinkedList<Integer> List1=new LinkedList<>();List1.addFirst(1);List1.addFirst(2);List1.addFirst(3);System.out.println(List1);        //[3, 2, 1]//带参数构造LinkedList<Integer> List2=new LinkedList<>(List1);List2.addFirst(99);System.out.println(List2);        //[99, 3, 2, 1]}
}
方法

解释

boolean add (E e)尾插 e
void add (int index,E element)将e插入到index位置
boolean addAll (Collection<? extends E> c)尾插c中的元素
E remove (int index)删除index位置元素

boolean remove (Object o)

删除遇到的第一个o
E get (int index)获取下标index位置元素
E set (int index,E element)将下标index位置元素设置为element
void clear ()清空
boolean contains (Object o)判断o是否在线性表
int indexOf (Object o)返回第一个o所在下标
int lastIndexOf (Object o)返回最后一个o的下标
List<E> subList (int fromIndex,int toIndex)截取部分list

2.2 LInkedList的遍历

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

http://www.lryc.cn/news/616133.html

相关文章:

  • MariaDB 数据库管理与web服务器
  • JUC学习笔记-----ReentrantLock
  • 通过trae开发你的第一个Chrome扩展插件
  • CST MATLAB 联合仿真超材料开口谐振环单元
  • Pytorch进阶-timm库-00快速开始
  • 数据结构(17)排序(下)
  • Excel常用功能函数
  • 深度相机---双目深度相机
  • CPP多态
  • 信号处理函数中调用printf时,遇到中断为什么容易导致缓冲区损坏?
  • 《广西棒垒球百科》男子垒球世界纪录·垒球2号位
  • 《算法导论》第 16 章 - 贪心算法
  • 【langchain】如何给langchain提issue和提pull request?
  • 机械学习--DBSCAN 算法(附实战案例)
  • 计算机网络:路由聚合是手动还是自动完成的?
  • 亚麻云之数据安家——RDS数据库服务入门
  • 人工智能-python-机器学习-决策树与集成学习:决策树分类与随机森林
  • LLIC:基于自适应权重大感受野图像变换编码的学习图像压缩
  • 结构化记忆、知识图谱与动态遗忘机制在医疗AI中的应用探析(上)
  • 网站SSL证书到期如何更换?简单完整操作指南
  • 计算机视觉(CV)——卷积神经网络基础
  • Spring WebFlux开发指导
  • [Shell编程] Shell的正则表达式
  • JS实现数组扁平化
  • Elasticsearch 搜索模板(Search Templates)把“可配置查询”装进 Mustache
  • 【AI学习从零至壹】AI调用MCP抓包分析pcap原始报文
  • Spring Boot 开发三板斧:POM 依赖、注解与配置管理
  • 我如何从安全运维逆袭成企业CSO
  • 专题二_滑动窗口_串联所有单词的子串
  • SQL约束:数据完整性的守护者