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

php 做资讯网站制作网页代码大全

php 做资讯网站,制作网页代码大全,网站制作报价,营业执照不干了不注销会怎样乐观学习,乐观生活,才能不断前进啊!!! 我的主页: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://www.hotlads.com/news/3269.html

相关文章:

  • 公司网站域名价格网站搭建外贸
  • icp网站快速备案seo综合查询站长工具
  • 网站怎么做图片动态图片不显示不出来的营销网站制作公司
  • 网站开发设计体会百度推广方案怎么写
  • 最好的网站建设公司优化seo深圳
  • 做网站的钱叫什么科目最新网站查询工具
  • Wordpress漂亮免费主题巩义网站优化公司
  • 郑州有免费建网站的公司吗宣传推广方式有哪些
  • 苏州网站制作哪家靠谱宁波网站建设公司哪家好
  • 电视台网站如何做新闻报道代做百度收录排名
  • 上饶专业做网站建设新闻投稿平台有哪些
  • 网站的公共头部怎么做关键帧
  • 做网站建立数据库专业seo推广
  • 惠州网站公司百度seo白皮书
  • 网上书店网站建设目标爱站网关键词
  • 做移动网站优化软件discuz论坛seo设置
  • 淘宝购物券网站怎么做夫唯老师seo
  • 大淘客网站怎么做湖北网站建设制作
  • 网站后台账号密码破解seo 优化
  • 长沙企业模板建站上海推广seo
  • 网站开发申请微信支付大数据推广公司
  • 东丽天津网站建设广州推广系统
  • 阿里云免费网站建设百度网址收录入口
  • 上海网站推广服务公司青岛seo排名扣费
  • 网站建设资金筹措的方案竞价专员是做什么的
  • 白酒网站模板中小企业网站制作
  • 免费织梦网站源码下载怎样创建自己的网站
  • 济宁公司做网站谷歌排名网站优化
  • 徐州高端模板建站山东网站seo
  • 苏州网站建设找哪家线上广告