Java链表的基本使用
生活随笔
收集整理的這篇文章主要介紹了
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屬性之中,而在進行輸出的時候也應該按照節點的先后順序,一個一個取得每一個節點所包裝的數據。
運行:
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链表的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。