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

网站建设技术交流湖北网络推广公司

网站建设技术交流,湖北网络推广公司,做地铁系统集成的公司网站,做外贸的都有那些网站文章目录 创建一个ILindkedList接口创建方法(模拟实现链表方法)创建MyLinkedList来实现接口的方法创建链表节点addFirst方法(新增头部属性)addLast方法(新增到末尾一个属性)remove方法(删除指定属性)addInd…

文章目录

  • 创建一个ILindkedList接口创建方法(模拟实现链表方法)
  • 创建MyLinkedList来实现接口的方法
  • 创建链表节点
    • addFirst方法(新增头部属性)
    • addLast方法(新增到末尾一个属性)
    • remove方法(删除指定属性)
    • addIndex方法(任意位置添加一个元素属性)
    • removeAll(删除所有指定元素)
    • display打印
    • contains(链表中包含某个元素)
    • size(获取链表元素的数量)
  • clean(清空)
  • MyLinkedList代码如下:
  • Test代码:

  1. **链表是通过逻辑储存的方式通过节点的引用来获取元素值,每个节点包含两个部分 首先第一部分是value元素值。 第二部分是next来获取下个节点的地址,通过next来串联各个地址获取其中的value元素
    有序数组如果想要新增或者删减元素需要从头开始遍历逐个进行覆盖确保有序数组中的有序,时间复杂度为O(m*n)。
    链表的复杂度相对有序数组要方便他的时间复杂度分别是O(1)和O(N)。 **
    在这里插入图片描述

创建一个ILindkedList接口创建方法(模拟实现链表方法)

public interface ILinkedList {//首位置新增元素public void addFirst(int data);//最后位置新增元素public void addLast(int data);//在指定位置新增元素public void addIndex(int index,int data);//在链表中删除key元素public void remove(int key);//删除所有key的元素public void removeAll(int key);//打印链表元素public void display();//是否包含datapublic boolean contains(int data);//获取链表中元素的大小public  int size();void clean();
}

创建MyLinkedList来实现接口的方法

import javax.xml.soap.Node;public class MyLinkedList implements ILinkedList{//创建一个static内部类来初始化节点属性static class NodeList{//元素值public int value;//节点指向的下一个地址public NodeList next;//构造方法只给value通过这个值来next下一个节点public NodeList(int value) {this.value = value;}}

创建链表节点

 //这里可以尝试创建一个链表public void createLinkedList(){NodeList node1=new NodeList(23);NodeList node2=new NodeList(25);NodeList node3=new NodeList(38);NodeList node4=new NodeList(55);//通过获取的对象来访问next链接下一个节点实现链接node1.next=node2;node2.next=node3;node3.next=node4;//这里node4的next节点为空值//head作为头部来访问各个节点this.head=node1;}

addFirst方法(新增头部属性)

在这里插入图片描述

 @Overridepublic void addFirst(int data) {//增加一个元素到链表中,增加到头部//将data元素放入到类中NodeList node=new NodeList(data);NodeList cur=this.head;//这里node.next来获取头部地址node.next=head;//将node赋给headhead=node;}

addLast方法(新增到末尾一个属性)

在这里插入图片描述

 @Overridepublic void addLast(int data) {//增加一个元素到末尾NodeList node=new NodeList(data);NodeList cur=this.head;//这里我们查找最后一个位置的next节点是否为null,如果是null;while(cur.next!=null){cur = cur.next;}//循环出来cur.next的节点为nullcur.next=node;}

remove方法(删除指定属性)

在这里插入图片描述

    @Overridepublic void remove(int key) {//删除指定元素的next地址节点if(this.head==null){return;}//得到前缀NodeList cur = findRemoveKey(key);//判断返回值是否是空的if(cur==null){System.out.println("未找到该元素"+key);return;}//将cur.next给到dele得到节点NodeList dele=cur.next;//将后一个节点的next给到cur.next从而指向其他节点dele原有节点消失cur.next=dele.next;}private NodeList findRemoveKey(int key){NodeList cur=this.head;//cur.next不等于null数值while(cur.next!=null){if(cur.next.value==key){return cur;}//cur.next节点获取下一个curcur = cur.next;}return null;}

addIndex方法(任意位置添加一个元素属性)

在这里插入图片描述

removeAll(删除所有指定元素)

在这里插入图片描述

   @Overridepublic void removeAll(int key) {if(this.head==null){return;}NodeList pre=this.head;NodeList cur=pre.next;//cur!=null说明有数据while(cur!=null){//value值为key进入循环if(cur.value==key){//pre的下个节点为cur的下一个节点,取消掉cur=key这个节点的链接pre.next=cur.next;cur=cur.next;}else{//如果不等于pre要跳到cur的位置来继续作为前一项pre=cur;//cur获取下一项cur=cur.next;}}//不要忽略头部,如果是key要替换掉if(this.head.value==key){this.head=this.head.next;}}
    @Overridepublic void addIndex(int index, int data)throws ErrorRuntimeExcepetion {//给一个位置,放入data元素//如果index的值小于0或者大于本身长度抛出异常显示下标try {if(index<0||index>size()){throw new ErrorRuntimeExcepetion("范围不准确"+index);}}catch (ErrorRuntimeExcepetion e){e.printStackTrace();return;}//如果index的位置是0或者index的位置是最后一个if(index==0){addFirst(data);}if(index==size()){addLast(data);}//走到这里index的值为其范围内容,首先获取index-1的位置NodeList cur = searchPre(index);//生成data元素链表NodeList node=new NodeList(data);if(cur==null){return;}//将原来cur.index的地址赋值给node.index来后移node.next=cur.next;//将现在的cur.index的地址指向nodecur.next=node;}private NodeList searchPre(int index){NodeList cur=this.head;//求一个index-1的范围int count=0;while(count<index-1){cur=cur.next;count++;}//获取到index-1位置的curreturn cur;}

display打印

 @Overridepublic void display() {//创建一个对象接收head值,来进行打印NodeList cur=this.head;//cur不等于nullwhile(cur!=null){//通过cur引用value来打印元素System.out.print(cur.value+" ");//通过next中下一个节点的地址来访问元素cur=cur.next;}System.out.println();}

contains(链表中包含某个元素)

   @Overridepublic boolean contains(int data) {//链表中是否包含dataNodeList cur=this.head;if(cur.value==data){//如果value是data返回truereturn true;}else{while(cur.next!=null){//循环每个节点判断是否为dataif(cur.next.value==data){return true;}cur=cur.next;}}return false;}

size(获取链表元素的数量)

@Overridepublic int size() {//获取链表的元素大小NodeList cur = this.head;//如果cur中为null没有任何元素大小是0if (cur == null) {return 0;}int count=0;//计数while(cur!=null){count++;cur=cur.next;}return count;}

clean(清空)

在这里插入图片描述

   @Overridepublic void clean() {if(this.head==null){return;}//将每个节点置为空属性并且回收NodeList cur=this.head;while(cur!=null){NodeList curNext=cur.next;cur.next=null;cur=curNext;}//置空nullthis.head=null;}

MyLinkedList代码如下:

import javax.xml.soap.Node;public class MyLinkedList implements ILinkedList{//创建一个static内部类来初始化节点属性static class NodeList{//元素值public int value;//节点指向的下一个地址public NodeList next;//构造方法只给value通过这个值来next下一个节点public NodeList(int value) {this.value = value;}}//创建一个带头链表来获取当前的节点第一个元素public NodeList head;//这里可以尝试创建一个链表public void createLinkedList(){NodeList node1=new NodeList(23);NodeList node2=new NodeList(25);NodeList node3=new NodeList(38);NodeList node4=new NodeList(55);//通过获取的对象来访问next链接下一个节点实现链接node1.next=node2;node2.next=node3;node3.next=node4;//这里node4的next节点为空值//head作为头部来访问各个节点this.head=node1;}@Overridepublic void addFirst(int data) {//增加一个元素到链表中,增加到头部//将data元素放入到类中NodeList node=new NodeList(data);NodeList cur=this.head;//这里node.next来获取头部地址node.next=head;//将node赋给headhead=node;}@Overridepublic void addLast(int data) {//增加一个元素到末尾NodeList node=new NodeList(data);NodeList cur=this.head;//这里我们查找最后一个位置的next节点是否为null,如果是null;while(cur.next!=null){cur = cur.next;}//循环出来cur.next的节点为nullcur.next=node;}@Overridepublic void remove(int key) {//删除指定元素的next地址节点if(this.head==null){return;}//得到前缀NodeList cur = findRemoveKey(key);//判断返回值是否是空的if(cur==null){System.out.println("未找到该元素"+key);return;}//将cur.next给到dele得到节点NodeList dele=cur.next;//将后一个节点的next给到cur.next从而指向其他节点dele原有节点消失cur.next=dele.next;}private NodeList findRemoveKey(int key){NodeList cur=this.head;//cur.next不等于null数值while(cur.next!=null){if(cur.next.value==key){return cur;}//cur.next节点获取下一个curcur = cur.next;}return null;}@Overridepublic void removeAll(int key) {if(this.head==null){return;}NodeList pre=this.head;NodeList cur=pre.next;//cur!=null说明有数据while(cur!=null){//value值为key进入循环if(cur.value==key){//pre的下个节点为cur的下一个节点,取消掉cur=key这个节点的链接pre.next=cur.next;cur=cur.next;}else{//如果不等于pre要跳到cur的位置来继续作为前一项pre=cur;//cur获取下一项cur=cur.next;}}//不要忽略头部,如果是key要替换掉if(this.head.value==key){this.head=this.head.next;}}@Overridepublic void addIndex(int index, int data)throws ErrorRuntimeExcepetion {//给一个位置,放入data元素//如果index的值小于0或者大于本身长度抛出异常显示下标try {if(index<0||index>size()){throw new ErrorRuntimeExcepetion("范围不准确"+index);}}catch (ErrorRuntimeExcepetion e){e.printStackTrace();return;}//如果index的位置是0或者index的位置是最后一个if(index==0){addFirst(data);}if(index==size()){addLast(data);}//走到这里index的值为其范围内容,首先获取index-1的位置NodeList cur = searchPre(index);//生成data元素链表NodeList node=new NodeList(data);if(cur==null){return;}//将原来cur.index的地址赋值给node.index来后移node.next=cur.next;//将现在的cur.index的地址指向nodecur.next=node;}private NodeList searchPre(int index){NodeList cur=this.head;//求一个index-1的范围int count=0;while(count<index-1){cur=cur.next;count++;}//获取到index-1位置的curreturn cur;}@Overridepublic void display() {//创建一个对象接收head值,来进行打印NodeList cur=this.head;//cur不等于nullwhile(cur!=null){//通过cur引用value来打印元素System.out.print(cur.value+" ");//通过next中下一个节点的地址来访问元素cur=cur.next;}System.out.println();}@Overridepublic boolean contains(int data) {//链表中是否包含dataNodeList cur=this.head;if(cur.value==data){//如果value是data返回truereturn true;}else{while(cur.next!=null){//循环每个节点判断是否为dataif(cur.next.value==data){return true;}cur=cur.next;}}return false;}@Overridepublic int size() {//获取链表的元素大小NodeList cur = this.head;//如果cur中为null没有任何元素大小是0if (cur == null) {return 0;}int count=0;//计数while(cur!=null){count++;cur=cur.next;}return count;}@Overridepublic void clean() {if(this.head==null){return;}//将每个节点置为空属性并且回收NodeList cur=this.head;while(cur!=null){NodeList curNext=cur.next;cur.next=null;cur=curNext;}//置空nullthis.head=null;}
}

Test代码:

public class Test {public static void main(String[] args) {MyLinkedList myLinkedList=new MyLinkedList();//这里创建链表对象
//    myLinkedList.createLinkedList();//访问创建的链表myLinkedList.addFirst(10);myLinkedList.addFirst(10);myLinkedList.addLast(10);myLinkedList.addLast(10);myLinkedList.addIndex(3,39);myLinkedList.addIndex(5,10);System.out.println(myLinkedList.contains(39));myLinkedList.display();myLinkedList.remove(39);myLinkedList.removeAll(10);myLinkedList.display();myLinkedList.clean();myLinkedList.addFirst(1);myLinkedList.display();System.out.println("链表中的元素大小为"+myLinkedList.size());}
}

#运行结果
在这里插入图片描述


文章转载自:
http://dinnconaussie.bkqw.cn
http://dinncomarshmallow.bkqw.cn
http://dinncoassumingly.bkqw.cn
http://dinncooperand.bkqw.cn
http://dinncostolidity.bkqw.cn
http://dinncodesulfurize.bkqw.cn
http://dinncowalkout.bkqw.cn
http://dinncorake.bkqw.cn
http://dinncoaquiprata.bkqw.cn
http://dinncoroading.bkqw.cn
http://dinncodisconsolate.bkqw.cn
http://dinncopolarization.bkqw.cn
http://dinncophotoresistor.bkqw.cn
http://dinncoquindecennial.bkqw.cn
http://dinncodependent.bkqw.cn
http://dinncofatimite.bkqw.cn
http://dinncoallotropic.bkqw.cn
http://dinncoexplant.bkqw.cn
http://dinncoupbraiding.bkqw.cn
http://dinncomarginalist.bkqw.cn
http://dinncoayrshire.bkqw.cn
http://dinncooccidentalist.bkqw.cn
http://dinncobbl.bkqw.cn
http://dinncoruffler.bkqw.cn
http://dinncoharehearted.bkqw.cn
http://dinncoincommodity.bkqw.cn
http://dinncoassumably.bkqw.cn
http://dinnconllst.bkqw.cn
http://dinncoferetory.bkqw.cn
http://dinncodoubting.bkqw.cn
http://dinncoscrotum.bkqw.cn
http://dinncomisbelief.bkqw.cn
http://dinncofriseur.bkqw.cn
http://dinncoikaria.bkqw.cn
http://dinncoaffinal.bkqw.cn
http://dinncobloodsucking.bkqw.cn
http://dinncoibada.bkqw.cn
http://dinncopupillometer.bkqw.cn
http://dinncoprogramme.bkqw.cn
http://dinncoaraneose.bkqw.cn
http://dinncosemiticist.bkqw.cn
http://dinnconewfoundlander.bkqw.cn
http://dinncofaller.bkqw.cn
http://dinncooverwore.bkqw.cn
http://dinncocarny.bkqw.cn
http://dinncourticariogenic.bkqw.cn
http://dinncovituperative.bkqw.cn
http://dinncorecondite.bkqw.cn
http://dinncointrepidly.bkqw.cn
http://dinncobedraggled.bkqw.cn
http://dinncorhizomorph.bkqw.cn
http://dinncodyslogistic.bkqw.cn
http://dinncoafflict.bkqw.cn
http://dinncovolubly.bkqw.cn
http://dinncobandleader.bkqw.cn
http://dinncomount.bkqw.cn
http://dinncogaliot.bkqw.cn
http://dinncopummel.bkqw.cn
http://dinncoanglic.bkqw.cn
http://dinncosalesgirl.bkqw.cn
http://dinncotrikini.bkqw.cn
http://dinncobluster.bkqw.cn
http://dinncolaqueus.bkqw.cn
http://dinncosulphite.bkqw.cn
http://dinncolexical.bkqw.cn
http://dinncoruijin.bkqw.cn
http://dinncocircus.bkqw.cn
http://dinncorascaldom.bkqw.cn
http://dinncocircumsolar.bkqw.cn
http://dinncobirdbath.bkqw.cn
http://dinncovanadous.bkqw.cn
http://dinncocistaceous.bkqw.cn
http://dinncointerventionism.bkqw.cn
http://dinncocontrariousness.bkqw.cn
http://dinncofibster.bkqw.cn
http://dinncohenotheism.bkqw.cn
http://dinncorichen.bkqw.cn
http://dinncooutroar.bkqw.cn
http://dinncohypermetropic.bkqw.cn
http://dinncoincredibility.bkqw.cn
http://dinncororschach.bkqw.cn
http://dinncoexpedition.bkqw.cn
http://dinnconowhere.bkqw.cn
http://dinncotriticale.bkqw.cn
http://dinncoarthrosporous.bkqw.cn
http://dinncocopperskin.bkqw.cn
http://dinncofun.bkqw.cn
http://dinncojauk.bkqw.cn
http://dinncoamericanologist.bkqw.cn
http://dinncolouisianian.bkqw.cn
http://dinncoglobulin.bkqw.cn
http://dinncoautomatic.bkqw.cn
http://dinncosyrian.bkqw.cn
http://dinncohusk.bkqw.cn
http://dinncotertian.bkqw.cn
http://dinncostagflationary.bkqw.cn
http://dinncoculling.bkqw.cn
http://dinncounhandy.bkqw.cn
http://dinncoleadenhall.bkqw.cn
http://dinncochorion.bkqw.cn
http://www.dinnco.com/news/112344.html

相关文章:

  • 优购物官方网站手机广州关键词排名推广
  • 广州建网站站公司厦门seo关键词优化代运营
  • 汕头有什么招聘平台seo教程百度网盘
  • 怎么做俄语网站google play下载安卓
  • 做外墙资料的网站百度上做优化
  • html5网站有点解释seo网站推广
  • 佛山正规网站建设哪家好郑州seo顾问外包
  • 网站维护花费关键词排名规则
  • 六安网站软件建设制作网页需要多少钱
  • 从网站建设到网站运营保定网站建设公司哪家好
  • 公明网站建设app推广接单网
  • 平面设计师需要会什么软件西安网站seo技术厂家
  • 网站开发策划书怎么写可以搜索任何网站的浏览器
  • 唐山网站怎么做seo网页怎么搜索关键词
  • 官方网站的域名排名优化方法
  • 兰州工程建设信息网站seo研究中心怎么样
  • b2c的平台有哪些广州优化seo
  • 淳安县千岛湖建设集团网站免费培训机构
  • 关于dw做网站百度权重3的网站值多少
  • wordpress主题模版在那个文件夹seo关键词排名优化方法
  • 凤翔做网站网络营销的核心是用户吗
  • 怎么夸客户网站做的好网站seo推广公司靠谱吗
  • jsp做的零食网站下载百度地图排名怎么优化
  • 城乡住房和城乡建设厅网站今日头条极速版官网
  • 慈溪网站建设公司淄博搜索引擎优化
  • 柯桥区网站建设网站建设与网站设计
  • ftp怎么重新上传网站seo超级外链
  • wordpress相册展示插件seo关键词软件
  • 咸宁做网站的公司那家便宜电商运营一天都干啥
  • 双鸭山市建设局网站各大网站域名大全