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

太原建站模板搭建破解版成年猫咪版永久

太原建站模板搭建,破解版成年猫咪版永久,室内设计公司排名一览表,佛山网站设计师目录 链表 1、链表的概念及结构 2、LinkedList的使用 2、1什么是LinkedList 2、2LinkedList的使用 3、LinkedList的遍历 4、LinkedList的模拟实现 5、ArrayList和LinkedList的区别 上篇已经熟悉了ArrayList的使用,ArrayList底层使用数组来存储元素。由于其底层…

目录

链表

1、链表的概念及结构

 2、LinkedList的使用

2、1什么是LinkedList

2、2LinkedList的使用

3、LinkedList的遍历

4、LinkedList的模拟实现

 5、ArrayList和LinkedList的区别


上篇已经熟悉了ArrayList的使用,ArrayList底层使用数组来存储元素。由于其底层是一段连续空间,当ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景

因此,java集合中又引入了LinkedList,即链表结构。

链表

1、链表的概念及结构

链表 是一种 物理存储结构上非连续 存储结构,数据元素的 逻辑顺序是通过链表中的引用链接次序实现的 。
实际中链表的结构非常多样,以下情况组合起来就有 8 种链表结构:

链表大分类

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

2、LinkedList的使用

2、1 什么是LinkedList

LinkedList官方文档

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

在集合框架中,LinkedList也实现了List接口,具体如下:

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

2、2 LinkedList的使用

LinkedList 的构造
方法解释
LinkedList ( )无参构造
Public LinkedList ( Collection<? extends E> c )
使用其他集合容器中元素构造List
 public static void main ( String [] args ) {
     // 构造一个空的 LinkedList
     List < Integer > list1 = new LinkedList <> ();
     List < String > list2 = new java . util . ArrayList <> ();
     list2 . add ( "JavaSE" );
     list2 . add ( "JavaWeb" );
     list2 . add ( "JavaEE" );
     // 使用 ArrayList 构造 LinkedList
    List < String > list3 = new LinkedList <> ( list2 );
 }

LinkedList常用方法 
方法解释
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(Obeject o)返回最后一个o 所在下标
List<E> subList(int fromIndex, int toIndex)截取部分 list

以上就是 LinkedList常用方法 ,需要多练熟悉用法

3、LinkedList的遍历

LinkedList 的遍历有三种;for循环 + 下标、 foreach 、使用迭代器
 LinkedList<Integer> list1=new LinkedList<>();list1.add(1);list1.add(2);list1.add(3);list1.add(4);

1、for循环遍历 

 for (int i = 0; i <list1.size() ; i++) {System.out.print(list1.get(i)+" ");}System.out.println(" ");

2、foreach遍历

  for (Integer i:list1){System.out.print(i+" ");}System.out.println(" ");

3、使用迭代器遍历---正向遍历

   ListIterator<Integer> it= list1.listIterator();while(it.hasNext())System.out.print(it.next()+" ");System.out.println(" ");

4、使用反向迭代器---反向遍历

ListIterator<Integer> rit= list1.listIterator(4);while(rit.hasPrevious())System.out.print(rit.previous()+" ");System.out.println(" ");}

4、LinkedList的模拟实现

模拟实现 LinkedList链表。

public class MyLinkedList {static class ListNode{public int val;public ListNode next;public ListNode prev;public ListNode(int val) {this.val = val;}}public ListNode head;public ListNode last;//头插法public void addFirst(int data){ListNode node=new ListNode(data);if(head==null) {head = last = node;return;}node.next=head;head.prev=node;head=node;}//尾插法public void addLast(int data){ListNode node=new ListNode(data);if(head==null) {head = last = node;return;}last=head;while(last.next!=null){last=last.next;}last.next=node;node.prev=last;last=last.next;}//任意位置插入,第一个数据节点为0号下标public boolean addIndex(int index,int data){int len=size();if(index<0||index>len) {System.out.println("index位置不合法");return false;}if(index==0) {addFirst(data);return true;}if(index==len) {addLast(data);return true;}ListNode cur=findnode(index);ListNode node=new ListNode(data);node.next=cur;cur.prev.next=node;node.prev=cur.prev;cur.prev=node;return true;}private ListNode findnode(int index){ListNode cur=head;while(index!=0){index--;cur=cur.next;}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){ListNode cur=head;while(cur!=null){if(cur.val==key){if(cur==head){head=head.next;if(head==null)return;}else {cur.prev.next=cur.next;if(cur.next==null){last=last.prev;}else{cur.next.prev=cur.prev;}}return;}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{cur.prev.next=cur.next;if(cur.next==null){last=last.prev;}else{cur.next.prev=cur.prev;}}}cur=cur.next;}}//得到单链表的长度public int size(){int count=0;ListNode cur=head;if(head==null)return 0;while(cur!=null){count++;cur=cur.next;}return count;}public void display(){ListNode cur=head;if(head==null)return;while(cur!=null){System.out.print(cur.val+" ");cur=cur.next;}System.out.println(" ");}public void clear(){ListNode cur=head,curN=head;while(cur!=null){curN=cur.next;cur.next=null;cur.prev=null;cur=curN;}head=last=null;}}

 5、ArrayListLinkedList的区别

不同点ArrayListLinkedList
存储空间上物理上一定连续逻辑上连续,物理上不一定连续
随机访问支持O(1)不支持 O ( n )
头插需要搬移元素,效率低 O( n )只需修改元素指向,时间复杂度 O(1)
插入空间不够可以扩容没有容量概念
应用场景元素高效存储+频繁访问任意位置插入和频繁删除

【数据结构】LinkedList与链表学习到这里

http://www.yayakq.cn/news/851316/

相关文章:

  • 如何买网站公众号下载wordpress
  • seo蒙牛伊利企业网站专业性诊断网站页面布局名称
  • 南阳网站排名公司基于jsp网站开发
  • 如何做网站数据报表erp软件是什么意思
  • 通信网站建设WordPress博客主题免费
  • 禹州做网站的python flask做网站
  • 开一个做网站的公司赚钱吗百度竞价托管代运营公司
  • 网页设计与网站建设...医院如何做网站策划
  • 烟台网站建设多少钱山东省中国建设银行网站
  • 内容转载的网站怎么做工作作风方面存在的问题及整改措施2023
  • 网站seo系统阿里邮箱登录入口
  • 网站qq在线代码电子制作网站
  • 河南小学网站建设cc0图片素材网站
  • 郑州正云网站建设外贸高端网站建设
  • 网站怎么弄缩略图上传黄骅市企业名录
  • 无锡高端网站设计公司价格网页设计与网站架设
  • 南京专业网站设计公司中小企业网络工程建设
  • 影响网站速度的代码qq邮箱登录手机版网页
  • 广西城乡建设网站床上用品网站源码
  • 公司网站开发建设免费流量网站推广
  • 织梦摄影网站模板html如何做购物网站
  • 网站建设管理专业介绍郑州做网站企起
  • 张家界商城网站开发设计自己做网站打开是乱码
  • 目前什么编码做网站最好物流网站建设策划书
  • 淘宝联盟推广网站建设贵州安顺网站建设
  • 建一个网站需要哪些人网站颜色搭配实例
  • 淘宝做推广网站定制软件公司
  • 政协网站建设情况汇报买机箱网站
  • 深圳找做网站室内装修设计软件用哪个好
  • 杭州高端品牌网站建设苏州最新情况最新消息今天