日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java链表的基本使用

發布時間:2024/9/20 java 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java链表的基本使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈表是一種根據元素節點邏輯關系排列起來的一種數據結構。利用鏈表可以保存多個數據,這一點類似于數組的概念,但是數組本身有一個缺點—— 數組的長度固定,不可改變,在長度固定的情況下首選的肯定是數組,但是在現實的開發之中往往要保存的內容長度是不確定的,那么此時就可以利用鏈表這樣的結構來代替數組的使用。

鏈表是一種最為簡單的數據結構,它的主要目的是依靠引用關系來實現多個數據的保存。

下面是定義一個簡單的類用來保存節點關系,并將所有節點鏈接起來。

例子1:

//每一個鏈表實際上就是由多個節點組成的 class Node { private String data; //用于保存數據private Node next; //用于保存下一個節點public Node(String data){ //每一個Node類對象都必須保存有數據this.data = data ;}public void setNext(Node next){this.next = next ;}public Node getNext(){return this.next ;}public String getData(){return this.data ;} }public class LinkedList {public static void main(String[] args) {//第一步:準備數據Node root = new Node("火車頭") ;Node n1 = new Node("車廂A") ;Node n2 = new Node("車廂B") ;// 鏈接節點root.setNext(n1);n1.setNext(n2);//第二步:取出所有數據Node currentNode = root ; //從當前根節點開始讀取while( currentNode != null){System.out.println(currentNode.getData()) ;//將下一個節點設置為當前節點scurrentNode = currentNode.getNext() ;}} }

運行:

火車頭 車廂A 車廂B

例子2:
在進行鏈表操作的時候,首先需要的是一個根節點(第一個節點即為根節點),之后每一個節點的引用都保存在上一節點的next屬性之中,而在進行輸出的時候也應該按照節點的先后順序,一個一個取得每一個節點所包裝的數據。

public class LinkedList {public static void main(String[] args) {Link link = new Link() ;link.add("hello"); //增加節點link.add("world");link.add("wwww");link.print(); //打印數據 } }//每一個鏈表實際上就是由多個節點組成的 class Node { private String data; // 用于保存數據private Node next; // 用于保存下一個節點public Node(String data) { // 每一個Node類對象都必須保存有響應的數據this.data = data;}public void setNext(Node next) {this.next = next;}public Node getNext() {return this.next;}public String getData() {return this.data;}// 實現節點的添加:// 第一次調用(Link):this代表Link.root// 第二次調用(Node):this代表Link.root.next// 第三次調用(Node):this代表Link.root.next.nextpublic void addNode(Node oldNode,Node newNode) { System.out.println("now:"+oldNode);if (this.next == null) { // 保存新節點this.next = newNode; } else { // 當前節點后面還有節點 this.next.addNode(this.next,newNode); // 當前節點的下一個節點繼續保存,這里采用的是遞歸添加節點}System.out.println(oldNode+"=>"+this.next);}// 第一次調用(Link):this代表Link.root// 第二次調用(Node):this代表Link.root.next// 第三次調用(Node):this代表Link.root.next.nextpublic void printNode() {System.out.println("pp:"+this.data);// 輸出當前數據if (this.next != null) { // 如果還有下一個節點,輸出下一節點this.next.printNode(); // 遞歸打印節點,注意這里的this.next中的this指代}} }// 鏈表增加節點,輸出節點數據 class Link {private Node root; //新建根節點public void add (String data){Node newNode = new Node(data); //鏈表中新增節點類對象 if(this.root == null ){ // 如果鏈表還沒有任何節點,就添加第一個節點作為根節點this.root = newNode; System.out.println("root:"+this.root);}else{ System.out.println("new:"+newNode);this.root.addNode(this.root,newNode); //從根節點節點新鏈接一個節點}}//輸出當前節點數據public void print(){if( this.root != null ){ this.root.printNode();}}}

運行:

root:test.Node@7852e922 new:test.Node@4e25154f now:test.Node@7852e922 test.Node@7852e922=>test.Node@4e25154f new:test.Node@70dea4e now:test.Node@7852e922 now:test.Node@4e25154f test.Node@4e25154f=>test.Node@70dea4e test.Node@7852e922=>test.Node@4e25154f pp:hello pp:world pp:wwww

例子3:

關鍵是構造一個類,里面包含一個指向下一個元素的對象(指向下一個元素的指針)

public class LinkedList{public static void main(String[] args){MyLinkedList linkedList = new MyLinkedList();System.out.println("-------start-------");System.out.println("ll:"+linkedList.listEm());for (int i=0;i<5;i++){ //新建鏈表linkedList.add(i+1);}System.out.println("mm:"+linkedList.listEm()); //打印鏈表for(int i=0;i<5;i++){ //刪除鏈表System.out.println("remove:"+linkedList.remove());}System.out.println("kk:"+linkedList.listEm());System.out.println("-------end-------");} }class Node<T> {Node<T> next;T element;public Node( Node<T> next, T element){this.next = next;this.element = element;} }class MyLinkedList<T> {private int size ; Node<T> last; //指向list中最后一個元素 Node<T> first; //指向list中第一個元素 Node<T> currRead; //指向當前讀取的元素 // 默認構造函數public MyLinkedList(){ this.size = 0;this.last = new Node(null,-1);this.first = last;this.currRead = first;}//往鏈表中添加數據(隊尾添加數據) public void add(T element){Node<T> newNode = new Node<T>(null,element);this.last.next = newNode;this.last = newNode; // 引用平移if(size == 0){this.first = newNode;}size ++;}//移除鏈表中的數據(隊頭移除)public T remove(){if(size == 0){System.out.println("empty list ");this.currRead = this.first;return null;}T result = this.first.element;this.first = this.first.next;this.currRead = this.first;size--;return result;}//獲取隊列中的元素public T get(){if(this.currRead.next == null){this.setReadAgain();return this.currRead.element;}T result = this.currRead.element;this.currRead = this.currRead.next;return result;}//再次從頭開始讀取數據public void setReadAgain() {this.currRead = this.first;}public String listEm(){StringBuilder sb = new StringBuilder();for(int i=0;i<size;i++){T ele = this.get();sb.append(this.currRead.element + "-->");}return sb.toString();}//獲取隊列大小public int getSize(){return this.size;}}

運行:

-------start------- ll: mm:1-->2-->3-->4-->5--> remove:1 remove:2 remove:3 remove:4 remove:5 kk: -------end-------

例四:

public class LinkedList {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("華曉風","jam",46); mytype duo=new mytype("余小風","duo",1000); mytype wang=new mytype("王秋","jack",21); mytype shi=new mytype("韓寒","bob",3000); mytype yu=new mytype("于冬","keven",30); l.add(dsome);//測試增加節點 l.add(shao); l.add(hua); l.add(wang); l.add(shi); l.add(duo); l.add(yu); System.out.println("鏈表長度:"+l.length());//鏈表長度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++){ //轉化為數組之后輸出 System.out.println(la[i].getInfo()); } System.out.println("\n利用索引方法輸出全部數據"); 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("執行清空操作后鏈表長度: "+l.length()+"\t是否為空鏈表:"+l.isEmpty()); } }class Link {//內部類 private class Node{ private Node next; private mytype data; public Node(mytype data){ this.data=data; } public void addNode(Node newNode){ //增加節點 if(this.next==null){ this.next=newNode; // 指向新的節點}else{ this.next.addNode(newNode); // 遞歸調用是為了讓next引用指向新的節點} } public mytype getNode(int index){//按照角標返回數據 if(index==Link.this.foot++){ return this.data; }else{ return this.next.getNode(index); } } public boolean iscontain(mytype data){//判斷是否含有該數據 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){ //刪除節點 if(this.data.equals(data)){ //this:下一個節點Bprevious.next=this.next; // this.next:節點B的下一個節點C,previous:節點A}else{ this.next.removeNode(this,data); //注意這里的this.next和this的區別:this.next是下一個節點B,this是當前節點A} } public void toArrayNode(){ //轉化數組 Link.this.Larray[Link.this.foot ++]=this.data; //每個節點的數據添加到一個mytype []中if(this.next!=null){ this.next.toArrayNode(); } } }//內部類定義完畢 private Node root; private int count=0; private int foot; private mytype [] Larray;public void add(mytype data){ //增加節點 if(data==null){ System.out.print("增加數據失敗,數據為空");//測試用 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(){//鏈表長度 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){//索引返回節點所存的數據 if(index>=this.count||index<0){ System.out.print("越界錯誤");//測試用 return null; }else{ this.foot=0; return this.root.getNode(index); } } public boolean contains(mytype data){ //判斷鏈表數據是否含data if(data==null) return false;elsereturn this.root.iscontain(data); } public void remove(mytype data){ //刪除指定數據節點 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("刪除錯誤");//測試用 } } public mytype[] toArray(){ //把鏈表轉化成對象數組 if(this.count==0){ return null; } this.foot=0; this.Larray=new mytype [this.count]; this.root.toArrayNode(); return this.Larray; } }class mytype {private String name; private String people; private int age;public mytype(String name,String people,int age){//鏈表中的數據(可自定義) this.name=name; this.people=people; this.age=age; } public boolean equals(mytype data){//判斷數據是否相同 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; } }

運行:

鏈表長度:7 名字 :韓敏 人物 :dsome 年齡 :21 名字 :邵曉 人物 :john 年齡 :45 名字 :華曉風 人物 :jam 年齡 :46 名字 :王秋 人物 :jack 年齡 :21 名字 :韓寒 人物 :bob 年齡 :3000 名字 :余小風 人物 :duo 年齡 :1000 名字 :于冬 人物 :keven 年齡 :30 是否包含余小風:true刪除多余后名字 :韓敏 人物 :dsome 年齡 :21 名字 :邵曉 人物 :john 年齡 :45 名字 :華曉風 人物 :jam 年齡 :46 名字 :王秋 人物 :jack 年齡 :21 名字 :韓寒 人物 :bob 年齡 :3000 名字 :于冬 人物 :keven 年齡 :30利用索引方法輸出全部數據 名字 :韓敏 人物 :dsome 年齡 :21 名字 :邵曉 人物 :john 年齡 :45 名字 :華曉風 人物 :jam 年齡 :46 名字 :王秋 人物 :jack 年齡 :21 名字 :韓寒 人物 :bob 年齡 :3000 名字 :于冬 人物 :keven 年齡 :30 是否包含多余:false執行清空操作后鏈表長度: 0 是否為空鏈表:true

參考:
https://blog.csdn.net/qq_37199582/article/details/79244657
https://blog.csdn.net/google_huchun/article/details/52824024

總結

以上是生活随笔為你收集整理的Java链表的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。