使用Java怎么插入链表结点
导读:本文共3502字符,通常情况下阅读需要12分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 为什么需要链表?我们知道,数组也可以存储数据,那么为什么还需要链表呢?接下来,我们来看看数组 和链表的区别:1、数组就像身上编了号站成一排的人,要找第10个人很容易,根据人身上的编号很快就能找到。但插入、删除慢,要往某个位置插入或删除一个人时,后面的人身上的编号都要变。当然,加入或删除的人始终末尾的也快。2、链表就像手牵着手站成一圈的人,要找第10个人不容易,必... ...
目录
(为您整理了一些要点),点击可以直达。为什么需要链表?
我们知道,数组也可以存储数据,那么为什么还需要链表呢?接下来,我们来看看数组 和链表的区别:
1、数组就像身上编了号站成一排的人,要找第10个人很容易,根据人身上的编号很快就能找到。但插入、删除慢,要往某个位置插入或删除一个人时,后面的人身上的编号都要变。当然,加入或删除的人始终末尾的也快。
2、链表就像手牵着手站成一圈的人,要找第10个人不容易,必须从第一个人一个个数过去。但插入、删除快。插入时只要解开两个人的手,并重新牵上新加进来的人的手就可以。删除一样的道理。
链表示意图
链表的建立
classTestLink{//创建一个外部类 privateEntryhead;//指向头结点的引用 publicTestLink(){ head=newEntry();//用结点类new一个头结点 } classEntry{//Entry创建一个结点内部类 intdata;//定义数据块 Entrynext;//定义地址块 publicEntry(){//构造方法1 data=-1;//对结点数据块初始化 next=null;//对地址初始化 } publicEntry(intval){//构造方法2 data=val;//对数据块赋值 next=null; } }}publicclassTestDemo2{ publicstaticvoidmain(String[]args){ TestLinktestlink=newTestLink(); //创建一个链表外部类对象}}
头插法:从头插入
publicvoidinsertHead(intval){//有这么一个结点Entrycur=newEntry(val);cur.next=head.next;head.next=cur;}
头插法示意图:
尾插法:从尾插入
publicvoidinsertTail(intval){ //找到尾巴 Entrycur=head; while(cur.next!=null){//遍历结点 cur=cur.next; } Entryentry=newEntry(val);//得到的结点 cur.next=entry; }
尾插法示意图:
从任意结点插入
publicbooleaninsertPos(intval,intpos){//1、判断pos的合法性if(pos<0||pos>=getLength()+1){returnfalse;}Entrycur=head;for(inti=0;i<=pos-1;i++){cur=cur.next;}//curpos的前一个Entryentry=newEntry(val);entry.next=cur.next;cur.next=entry;returntrue;}
示意图:
完整代码:
packageLianBiao;classTestLink1{ privateEntryhead;//指向头结点的引用 publicTestLink1(){ head=newEntry(); } classEntry{//EntryNode intdata; Entrynext; publicEntry(){ data=-1; next=null; } publicEntry(intval){ data=val; next=null; } } publicvoidinsertHead(intval){ //有这么一个结点 Entrycur=newEntry(val); cur.next=head.next; head.next=cur; /*head.next=cur; cur.next=head.next;*/ } publicvoidinsertTail(intval){ //找到尾巴 Entrycur=head; while(cur.next!=null){ cur=cur.next; } Entryentry=newEntry(val);//得到的结点 cur.next=entry; } //得到单链表的长度: publicintgetLength(){ intlen=0; Entrycur=head.next; while(cur!=null){ len++; cur=cur.next; } returnlen; } //将数据插入到指定位置 publicbooleaninsertPos(intval,intpos){ //1、判断pos的合法性 if(pos<0||pos>=getLength()+1){ returnfalse; } Entrycur=head; for(inti=0;i<=pos-1;i++){ cur=cur.next; } //curpos的前一个 Entryentry=newEntry(val); entry.next=cur.next; cur.next=entry; returntrue; } // //show() publicvoidshow(){ /*Entrycur=head; while(cur.next!=null){ System.out.println("data:"+cur.next.data); cur=cur.next; }*/ Entrycur=head.next; while(cur!=null){ System.out.println("data:"+cur.data); cur=cur.next; } }}publicclassLianBiao1{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub TestLink1testlink=newTestLink1(); testlink.insertTail(1330); testlink.insertTail(110); //1330110 testlink.insertPos(10,0); //101330110 if(testlink.insertPos(32,10000)){ System.out.println("插入成功"); }else{ System.out.println("插入失败"); } //10321330110 testlink.show(); System.out.println(testlink.getLength()); } }
输出结果:
补充:java中创建链表,实现链表的尾部插入
我就废话不多说了,大家还是直接看代码吧~
packagetest;//目标:创建链表,实现链表结点的尾部插入classNode_5{privateStringdata;publicNode_5nextNode;publicvoidsetData(Stringindata){this.data=indata;}publicStringgetData(){returnthis.data;}publicvoidsetNextNode(Node_5newNode){this.nextNode=newNode;}publicNode_5getNextNode(){returnthis.nextNode;}publicvoidaddData(Stringindata){setData(indata);Node_5node_5=newNode_5();Node_5head=node_5;if(node_5.getData()==null){node_5.setData(indata);System.out.println(node_5.getData());}else{node_5.setNextNode(node_5);node_5.setData(indata);System.out.println(node_5.getData());}}}publicclassT_5{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubNode_5node_5=newNode_5();for(inti=1;i<=3;i++){node_5.addData("第"+i+"结点");}}}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
使用Java怎么插入链表结点的详细内容,希望对您有所帮助,信息来源于网络。