首页 > Java > java教程 > 详解java中链表的定义与使用的示例代码

详解java中链表的定义与使用的示例代码

黄舟
发布: 2017-03-25 10:31:52
原创
1376 人浏览过

这篇文章主要介绍了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

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

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

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());

}

}

 

 

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();

    }

   

}

 

//内部类定义完毕

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;    

   }    

}

 

 

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

登录后复制

以上是详解java中链表的定义与使用的示例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板