一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 鏈表的定義與簡單實例

Java 鏈表的定義與簡單實例

2020-11-24 15:03Java之家 Java教程

這篇文章主要介紹了 Java 鏈表的定義與簡單實例的相關資料,需要的朋友可以參考下

 Java 鏈表的定義與簡單實例

Java實現鏈表主要依靠引用傳遞,引用可以理解為地址,鏈表的遍歷多使用遞歸,這里我存在一個疑問同一個類的不同對象的的相同方法的方法內調用算不算遞歸.

這里我寫的是單向鏈表;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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("華曉風","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());
}
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.java;
public 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);
   }
  }
 
  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)){
    previous.next=this.next;
 
   }else{
    this.next.removeNode(this,data);
   }
  }
 
 
  public void toArrayNode(){//轉化數組
    Link.this.Larray[Link.this.foot ++]=this.data;
    if(this.next!=null){
     this.next.toArrayNode();
    }
   
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//內部類定義完畢
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;
    return 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;    
   }    
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.example.java;
 
public 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;
 }  
}

測試效果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
鏈表長度: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
 

原文鏈接:http://blog.csdn.net/google_huchun/article/details/52824024

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产极品久久 | 国内精品伊人久久大香线焦 | 久久国产精品二区99 | 青草国内精品视频在线观看 | 国产亚洲精品91 | 亚洲 日韩 在线 国产 视频 | 天堂一区二区在线观看 | 日韩一区二三区无 | 996热精品视频在线观看 | japan孕妇孕交 | 女人和男人搞鸡 | 欧美男男gaygayxxx| 亚洲国产综合精品 | 日本一本二本三区免费 | 国产福利一区二区精品视频 | 荷兰精品女人性hd | 好大好深受不了了快进来 | 国产精品日本一区二区不卡视频 | 精品国产精品人妻久久无码五月天 | 无套内射在线观看THEPORN | 免费操比视频 | 美女逼逼喷水 | www日本视频| 亚洲精品久久中文字幕 | 俄罗斯激情性孕妇孕交大全 | 精彩国产萝视频在线 | 国产卡一卡二卡四卡无卡 | 亚洲国产在线观看免费视频 | 能看的毛片网站 | 欧洲美女人牲交一级毛片 | 我与白丝同桌的故事h文 | 日日射视频 | 白丝校花掀起短裙呻吟小说 | 女人张开腿 让男人桶个爽 免费观看 | 亚洲视频久久 | gayrb免费漫画入口 | 果冻传媒在线播放观看228集 | 国产综合视频 | 国产成人综合久久精品红 | 亚洲区精品久久一区二区三区 | 2018亚洲男人天堂 |