Java 鏈表的定義與簡(jiǎn)單實(shí)例
更新時(shí)間:2017年06月26日 09:55:00 投稿:lqh
這篇文章主要介紹了 Java 鏈表的定義與簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
Java 鏈表的定義與簡(jiǎn)單實(shí)例
Java實(shí)現(xiàn)鏈表主要依靠引用傳遞,引用可以理解為地址,鏈表的遍歷多使用遞歸,這里我存在一個(gè)疑問同一個(gè)類的不同對(duì)象的的相同方法的方法內(nèi)調(diào)用算不算遞歸.
這里我寫的是單向鏈表;
package com.example.java; public class MyLink { public static void main(String [] args){ Link l=new Link(); mytype[] la; mytype dsome=new mytype("韓敏","dsome",21); mytype shao=new mytype("邵曉","john",45); mytype hua=new mytype("華曉風(fēng)","jam",46); mytype duo=new mytype("余小風(fēng)","duo",1000); mytype wang=new mytype("王秋","jack",21); mytype shi=new mytype("韓寒","bob",3000); mytype yu=new mytype("于冬","keven",30); l.add(dsome);//測(cè)試增加節(jié)點(diǎn) l.add(shao); l.add(hua); l.add(wang); l.add(shi); l.add(duo); l.add(yu); System.out.println("鏈表長(zhǎng)度:"+l.length());//鏈表長(zhǎng)度 la=l.toArray(); for(int i=0;i<la.length;i++){ System.out.println(la[i].getInfo()); } System.out.println("是否包含多余:"+l.contains(duo)+"\n"); System.out.println("刪除多余后\n"); l.remove(duo); la=l.toArray(); for(int i=0;i<la.length;i++){//轉(zhuǎn)化為數(shù)組之后輸出 System.out.println(la[i].getInfo()); } System.out.println("\n利用索引方法輸出全部數(shù)據(jù)"); for(int i=0;i<l.length();i++){ System.out.println(l.get(i).getInfo()); } System.out.println("是否包含多余:"+l.contains(duo)+"\n"); l.clean(); System.out.println("執(zhí)行清空操作后鏈表長(zhǎng)度: "+l.length()+"\t是否為空鏈表:"+l.isEmpty()); } }
package com.example.java; public class Link { private class Node{//內(nèi)部類 private Node next; private mytype data; public Node(mytype data){ this.data=data; } public void addNode(Node newNode){//增加節(jié)點(diǎn) if(this.next==null){ this.next=newNode; }else{ this.next.addNode(newNode); } } public mytype getNode(int index){//按照角標(biāo)返回?cái)?shù)據(jù) if(index==Link.this.foot++){ return this.data; }else{ return this.next.getNode(index); } } public boolean iscontain(mytype data){//判斷是否含有該數(shù)據(jù) if(this.data.equals(data)){ return true; }else{ if(this.next!=null){ return this.next.iscontain(data); }else{ return false; } } } public void removeNode(Node previous,mytype data){//刪除節(jié)點(diǎn) if(this.data.equals(data)){ previous.next=this.next; }else{ this.next.removeNode(this,data); } } public void toArrayNode(){//轉(zhuǎn)化數(shù)組 Link.this.Larray[Link.this.foot ++]=this.data; if(this.next!=null){ this.next.toArrayNode(); } } }
//內(nèi)部類定義完畢 private Node root; private int count=0; private int foot; private mytype [] Larray; public void add(mytype data){//增加節(jié)點(diǎn) if(data==null){ System.out.print("增加數(shù)據(jù)失敗,數(shù)據(jù)為空");//測(cè)試用 return; } Node newNode=new Node(data); if(this.root==null){ this.root=newNode; this.count++; }else{ this.root.addNode(newNode); this.count++; } } public int length(){//鏈表長(zhǎng)度 return this.count; } public boolean isEmpty(){//是否為空鏈表 if(this.count==0)return true; else return false; } public void clean(){//清空鏈表 this.root=null; this.count=0; } public mytype get(int index){//索引返回節(jié)點(diǎn)所存的數(shù)據(jù) if(index>=this.count||index<0){ System.out.print("越界錯(cuò)誤");//測(cè)試用 return null; }else{ this.foot=0; return this.root.getNode(index); } } public boolean contains(mytype data){//判斷鏈表數(shù)據(jù)是否含data if(data==null) return false; return this.root.iscontain(data); } public void remove(mytype data){//刪除指定數(shù)據(jù)節(jié)點(diǎn) if(this.contains(data)){ if(this.root.data.equals(data)){ this.root=this.root.next; this.count--; } else{ this.count--; this.root.next.removeNode(root,data); } }else{ System.out.print("刪除錯(cuò)誤");//測(cè)試用 } } public mytype[] toArray(){//把鏈表轉(zhuǎn)化成對(duì)象數(shù)組 if(this.count==0){ return null; } this.foot=0; this.Larray=new mytype [this.count]; this.root.toArrayNode(); return this.Larray; } }
package com.example.java; public class mytype { private String name; private String people; private int age; public mytype(String name,String people,int age){//鏈表中的數(shù)據(jù)(可自定義) this.name=name; this.people=people; this.age=age; } public boolean equals(mytype data){//判斷數(shù)據(jù)是否相同 if(this==data){ return true; } if(data==null){ return false; } if(this.name.equals(data.name)&&this.people.equals(data.people)&&this.age==data.age){ return true; }else{ return false; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPeople() { return people; } public void setPeople(String people) { this.people = people; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getInfo(){ return "名字 :"+this.name+"\n"+ "人物 :"+this.people+"\n"+ "年齡 :"+this.age; } }
測(cè)試效果如下:
鏈表長(zhǎng)度:7 名字 :韓敏 人物 :dsome 年齡 :21 名字 :邵曉 人物 :john 年齡 :45 名字 :華曉風(fēng) 人物 :jam 年齡 :46 名字 :王秋 人物 :jack 年齡 :21 名字 :韓寒 人物 :bob 年齡 :3000 名字 :余小風(fēng) 人物 :duo 年齡 :1000 名字 :于冬 人物 :keven 年齡 :30 是否包含多余:true 刪除多余后 名字 :韓敏 人物 :dsome 年齡 :21 名字 :邵曉 人物 :john 年齡 :45 名字 :華曉風(fēng) 人物 :jam 年齡 :46 名字 :王秋 人物 :jack 年齡 :21 名字 :韓寒 人物 :bob 年齡 :3000 名字 :于冬 人物 :keven 年齡 :30 利用索引方法輸出全部數(shù)據(jù) 名字 :韓敏 人物 :dsome 年齡 :21 名字 :邵曉 人物 :john 年齡 :45 名字 :華曉風(fēng) 人物 :jam 年齡 :46 名字 :王秋 人物 :jack 年齡 :21 名字 :韓寒 人物 :bob 年齡 :3000 名字 :于冬 人物 :keven 年齡 :30 是否包含多余:false 執(zhí)行清空操作后鏈表長(zhǎng)度: 0 是否為空鏈表:true
相關(guān)文章
Java?HashMap詳解及實(shí)現(xiàn)原理
Java?HashMap是Java集合框架中最常用的實(shí)現(xiàn)Map接口的數(shù)據(jù)結(jié)構(gòu),它使用哈希表實(shí)現(xiàn),允許null作為鍵和值,可以存儲(chǔ)不同類型的鍵值對(duì),在Java中,HashMap被廣泛應(yīng)用于各種場(chǎng)景,如緩存、數(shù)據(jù)庫(kù)連接池、路由器等,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-05-05java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01使用開源項(xiàng)目JAVAE2 進(jìn)行視頻格式轉(zhuǎn)換
這篇文章主要介紹了使用開源項(xiàng)目JAVAE 進(jìn)行視頻格式轉(zhuǎn)換,幫助大家更好的利用Java處理視頻,完成自身需求,感興趣的朋友可以了解下2020-11-11詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Java線程中sleep和wait的區(qū)別詳細(xì)介紹
Java中的多線程是一種搶占式的機(jī)制,而不是分時(shí)機(jī)制。搶占式的機(jī)制是有多個(gè)線程處于可運(yùn)行狀態(tài),但是只有一個(gè)線程在運(yùn)行2012-11-11