java 中链表的定义与使用方法
作者:湖春 时间:2023-03-07 07:34:13
java 中链表的定义与使用方法
Java实现链表主要依靠引用传递,引用可以理解为地址,链表的遍历多使用递归,这里我存在一个疑问同一个类的不同对象的的相同方法的方法内调用算不算递归.
这里我写的是单向链表;
实例代码:
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;
}
}
测试效果如下:
链表长度: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
标签:java,链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
你都理解创建线程池的参数吗?
2022-06-10 06:36:05
![](https://img.aspxhome.com/file/2023/2/61662_0s.jpg)
dubbo入门指南及demo实例详解
2023-08-24 04:49:07
![](https://img.aspxhome.com/file/2023/6/67166_0s.jpg)
Android利用ViewDragHelper轻松实现拼图游戏的示例
2022-07-10 08:57:06
![](https://img.aspxhome.com/file/2023/6/95966_0s.gif)
C#中datagridview的EditingControlShowing事件用法实例
2021-05-27 23:08:43
C++实现企业职工工资管理系统
2023-11-02 17:34:34
Java解决约瑟夫问题代码实例
2023-09-20 19:17:02
Android使用SharedPreferences存储数据的实现方法
2021-08-22 02:05:49
Mapreduce分布式并行编程
2023-04-30 02:43:16
![](https://img.aspxhome.com/file/2023/4/97814_0s.jpg)
Java 将List中的实体类按照某个字段进行分组并存放至Map中操作
2022-09-13 20:25:35
![](https://img.aspxhome.com/file/2023/0/82300_0s.jpg)
SpringBoot整合canal实现数据同步的示例代码
2022-05-07 19:51:24
![](https://img.aspxhome.com/file/2023/1/61661_0s.png)
Android仿银行客户签名并且保存签名的截图文件并命名为本地时间
2023-09-19 22:42:48
基于Spring Security前后端分离的权限控制系统问题
2022-10-16 21:32:39
![](https://img.aspxhome.com/file/2023/4/66254_0s.png)
简单谈谈java的异常处理(Try Catch Finally)
2021-08-01 12:40:02
最最常用的 100 个 Java类分享
2023-08-21 08:45:05
![](https://img.aspxhome.com/file/2023/3/103863_0s.jpg)
Java Grpc实例创建负载均衡详解
2022-03-07 17:50:15
![](https://img.aspxhome.com/file/2023/7/81947_0s.png)
springboot实现注册加密与登录解密功能(demo)
2021-12-15 15:10:08
![](https://img.aspxhome.com/file/2023/4/77484_0s.png)
Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍
2021-10-11 00:51:28
Java如何跳过https的ssl证书验证详解
2023-08-24 11:34:56
![](https://img.aspxhome.com/file/2023/4/58094_0s.jpg)
Java中两个List之间的比较方法(差集、交集和并集)
2023-03-06 06:34:42
详解Java Map中三个冷门容器的使用
2021-09-02 14:27:22
![](https://img.aspxhome.com/file/2023/3/119073_0s.png)