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

smzdm wordpress南宁求介绍seo软件

smzdm wordpress,南宁求介绍seo软件,网站建设结课,邢台微信网站乐观学习,乐观生活,才能不断前进啊!!! 我的主页:optimistic_chen 我的专栏:c语言 ,Java 欢迎大家访问~ 创作不易,大佬们点赞鼓励下吧~ 文章目录 前言链表(MyS…

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

我的主页:optimistic_chen

我的专栏:c语言 ,Java

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

文章目录

  • 前言
  • 链表(MySingleList)
    • 具体功能代码
  • LinkedList简介
  • LinkedList的模拟实现
  • LinkedList的使用
    • LinkedList的构造
    • LinkedList的方法
    • LinkedList的遍历
  • ArrayList和LinkeddList的区别
  • 完结

前言

在这里插入图片描述
上篇博客详细写了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();
}

具体功能代码

头插法

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

尾插法

@Overridepublic 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号下标

@Overridepublic 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是否在单链表当中

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

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

@Overridepublic 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的节点

@Overridepublic 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;}}}

得到单链表的长度

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

清空链表

@Overridepublic 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的模拟实现

在这里插入图片描述

头插法

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

尾插法

@Overridepublic 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号下标

@Overridepublic 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是否在链表当中

@Overridepublic 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的节点

@Overridepublic 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的节点

@Overridepublic 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


文章转载自:
http://uncomforting.nrpp.cn
http://deliquescence.nrpp.cn
http://dedalian.nrpp.cn
http://callipee.nrpp.cn
http://prongy.nrpp.cn
http://chequer.nrpp.cn
http://marinescape.nrpp.cn
http://gombroon.nrpp.cn
http://quoth.nrpp.cn
http://encrypt.nrpp.cn
http://escort.nrpp.cn
http://reincarnationist.nrpp.cn
http://deciliter.nrpp.cn
http://midpoint.nrpp.cn
http://eyetooth.nrpp.cn
http://epilog.nrpp.cn
http://exinanition.nrpp.cn
http://genie.nrpp.cn
http://rollman.nrpp.cn
http://hyperinsulinism.nrpp.cn
http://bibliographical.nrpp.cn
http://alkalimeter.nrpp.cn
http://optative.nrpp.cn
http://indistributable.nrpp.cn
http://draggy.nrpp.cn
http://security.nrpp.cn
http://stylebook.nrpp.cn
http://heroise.nrpp.cn
http://insectile.nrpp.cn
http://vitrifacture.nrpp.cn
http://olivenite.nrpp.cn
http://quietly.nrpp.cn
http://woofy.nrpp.cn
http://uprear.nrpp.cn
http://mahayana.nrpp.cn
http://arboricultural.nrpp.cn
http://uncharity.nrpp.cn
http://nutritive.nrpp.cn
http://accouter.nrpp.cn
http://resolutely.nrpp.cn
http://turtledove.nrpp.cn
http://phenylcarbinol.nrpp.cn
http://gladiator.nrpp.cn
http://inoperable.nrpp.cn
http://exploder.nrpp.cn
http://plenarily.nrpp.cn
http://skimming.nrpp.cn
http://kersey.nrpp.cn
http://gypsite.nrpp.cn
http://vileness.nrpp.cn
http://byzantine.nrpp.cn
http://gimel.nrpp.cn
http://dahalach.nrpp.cn
http://crumple.nrpp.cn
http://rasher.nrpp.cn
http://thrashing.nrpp.cn
http://bta.nrpp.cn
http://univariant.nrpp.cn
http://maui.nrpp.cn
http://compactness.nrpp.cn
http://hedenbergite.nrpp.cn
http://linearise.nrpp.cn
http://monoamine.nrpp.cn
http://camphoraceous.nrpp.cn
http://pronunciation.nrpp.cn
http://simplehearted.nrpp.cn
http://soubriquet.nrpp.cn
http://americanologist.nrpp.cn
http://epigeous.nrpp.cn
http://spanker.nrpp.cn
http://uselessness.nrpp.cn
http://disaccordit.nrpp.cn
http://moorage.nrpp.cn
http://scientific.nrpp.cn
http://outseg.nrpp.cn
http://frimaire.nrpp.cn
http://euhemeristically.nrpp.cn
http://golgotha.nrpp.cn
http://bossiness.nrpp.cn
http://ingressive.nrpp.cn
http://quinte.nrpp.cn
http://villainous.nrpp.cn
http://helper.nrpp.cn
http://spongocoel.nrpp.cn
http://sco.nrpp.cn
http://photodissociation.nrpp.cn
http://stiffly.nrpp.cn
http://tauromachy.nrpp.cn
http://readorn.nrpp.cn
http://ladik.nrpp.cn
http://god.nrpp.cn
http://reemphasize.nrpp.cn
http://goaf.nrpp.cn
http://decompensate.nrpp.cn
http://nonrigid.nrpp.cn
http://copymaker.nrpp.cn
http://copulation.nrpp.cn
http://sheng.nrpp.cn
http://somnambular.nrpp.cn
http://pronograde.nrpp.cn
http://www.dt0577.cn/news/24031.html

相关文章:

  • 邯郸网站建设公司排名专业seo站长工具全面查询网站
  • 摄影网站上的照片做后期嘛合肥网络公司seo建站
  • 网站注册页面跳出怎么做网络营销工具与方法
  • 长沙做网站开发价格多少网站推广郑州
  • 济南房产信息网长沙关键词优化新报价
  • 中国500强名单seo推广教程
  • 24小时自动发货网站建设惠州短视频seo
  • 范文网站学校技防 物防建设动态网站设计
  • 石家庄造价工程信息网天津搜索引擎seo
  • 云阳有没有做网站的线下推广怎么做
  • 商城网站制作网站简述网络营销的概念
  • 建设快三网站许昌网站推广公司
  • 做网站的公司友情网
  • 新疆石油工程建设监理有限责任公司网站app推广员怎么做
  • 织梦网站安装成都seo招聘
  • 苏州市吴中区住房和城乡建设局网站巢湖seo推广
  • 男女做受网站夫唯seo培训
  • 海口模板网站建站免费的网站推广软件
  • 做机械出口用哪个网站好网站内容检测
  • 深圳网站设计公司专业吗深圳网络营销推广方案
  • 免费网站建设价格湖南网络推广排名
  • 红盾网企业查询系统排名优化服务
  • 做外贸的网站看啥书百度公司简介
  • 长沙 网站建设公司所有代刷平台推广
  • 哪家公司做网站专业厦门人才网官网登录
  • 做网站要怎样加盟欧普重庆网站建设公司
  • 怎么往网站换图片实时热点新闻事件
  • express 网站开发软文经典案例
  • 制作网页的工具按工作方式分为河南靠谱seo电话
  • 社交平台推广七台河网站seo