源码实现
package cn.Link;
interface ILink<E>
{
public void add(E e);//链表数据增加
public int count();//获取链表元素个数
public boolean isEmpty();//空集合判断
public Object[] toArray();//返回链表数据
public E get(int index);//根据索引取得数据
public void set(int index,E data);//修改链表数据
public boolean contains(E data);//数据内容查询
public void remove(E data);//删除链表数据:1.删除根节点,2.删除子节点
public void clean();//清空链表数据
}
class LinkImple<E> implements ILink<E>
{
private int count;
private Node root;
private int foot;
private Object [] returnData;
private class Node
{
private E data;
private Node next;
public Node(E data){//节点存储数据操作
this.data=data;
}
public void addNode(Node newNode){//节点存储下一个节点操作
if(this.next==null){//当前对象的下一个节点内存为空时,把传入的节点对象赋给当前对象的下一个节点内存中
this.next=newNode;
}else{//当前对象的下一个节点内存非空时,通过递归调用来实现节点对象的保存
this.next.addNode(newNode);
}
}
public void toArrayNode(){
LinkImple.this.returnData[LinkImple.this.foot++]=this.data;
if(this.next!=null){
this.next.toArrayNode();
}
}
public E getNode(int index){
if(LinkImple.this.foot++ == index){
return this.data;
}else{
return this.next.getNode(index);//将此处得到的数据再用一个return返回到调用处
}
}
public void setNode(int index,E data){
if(LinkImple.this.foot++ == index){
this.data=data;
}else{
this.next.setNode(index,data);
}
}
public boolean containsNode(E data){
if(data.equals(this.data)){
return true;
}else if(this.next==null){
return false;
}else{
return this.next.containsNode(data);
}
}
public void removeNode(Node previous,E data){//删除子节点
if(data.equals(this.data)){
previous.next=this.next;
}else if(this.next!=null){
this.next.removeNode(this,data);
}
}
}
public void add(E e){
if(e==null){
return ;
}
Node newNode=new Node(e);
if(this.root==null){
root=newNode;
}else{
this.root.addNode(newNode);
}
count++;
}
public int count(){
return this.count;
}
public boolean isEmpty(){
//return this.root==null;
return this.count==0;
}
public Object[] toArray(){
if(this.isEmpty()){
return null;
}
this.foot=0;
this.returnData=new Object[this.count];
this.root.toArrayNode();
return returnData;
}
public E get(int index){
if(index>=count){
return null;
}
this.foot=0;
return this.root.getNode(index);
}
public void set(int index,E data){
if(index>=count){
return ;
}
this.foot=0;
this.root.setNode(index,data);
}
public boolean contains(E data){
if(data==null){
return false;
}
return this.root.containsNode(data);
}
public void remove(E data){//删除根节点
if(data==null){
return ;
}
if(data.equals(this.root.data)){
this.root=this.root.next;
}else{
this.root.removeNode(this.root,data);
}
count--;
}
public void clean(){
this.root=null;
count=0;
}
}
public class Link
{
public static void main(String agrs[]){
ILink <String>link=new LinkImple <String> ();
System.out.println("初始化数据:"+link.count()+"\t 是否为空集合:"+link.isEmpty());
link.add("hello");
link.add("world");
link.add("!");
System.out.println("结果数据:"+link.count()+"\t 是否为空集合:"+link.isEmpty());
Object[] result=link.toArray();
for(Object temp:result){
System.out.println(temp);
}
System.out.println("---------------------根据索引获取链表元素--------------------");
System.out.println(link.get(1));
System.out.println("---------------------根据索引修改链表元素--------------------");
link.set(1,"WORLD");
System.out.println(link.get(1));
System.out.println("---------------------根据数组盘判断是否存在对应链表元素--------------------");
System.out.println(link.contains("WORLD"));
System.out.println("---------------------根据自动内容删除对应链表元素--------------------");
link.remove("WORLD");
Object[] resultB=link.toArray();
for(Object temp:resultB){
System.out.println(temp);
}
System.out.println("---------------------清空链表元素--------------------");
link.clean();
System.out.println(link.isEmpty());
}
}
运行结果
初始化数据:0 是否为空集合:true
结果数据:3 是否为空集合:false
hello
world
!
---------------------根据索引获取链表元素--------------------
world
---------------------根据索引修改链表元素--------------------
WORLD
---------------------根据数组盘判断是否存在对应链表元素--------------------
true
---------------------根据自动内容删除对应链表元素--------------------
hello
!
---------------------清空链表元素--------------------
true
Process finished with exit code 0
源码分析
【ILink接口】:统一了链表操作规范,并在隐藏操作细节的同时暴露了链表操作的方法。
【LinkImple】:用于实现ILink中的方法,在其中拥有一个私有内部类 Node结点类,通过该类保存结点信息,该内部类中还有供外部类LinkImple调用的方法如:addNode,toArrayNode,getNode,setNode,removeNode,containsNode…这些方法都沿用了递归的思想,通过Node对象与Node的Next对象,不断递归找到条件满足的状态,实现具体的功能。这些方法给外部类LinkImple的实现方法来使用。
【add方法实现】:外部类先剔除空数据后,开辟一个Node类对象,判定根节点root是否为空,空就对象赋予root,否则通过内部类的addNode方法利用递归的形式找到空结点并赋值。
【count方法实现】:在外部类中有count变量,每当add一个数据时,就加一,在外部类count方法中只需要返回count值即可。
【isEmpty方法实现】:判断count数据是否为0即可。
【toArray方法实现】:在外部类中有一个Object类型的数组叫returnData[],外部类toArray方法中调用isEmpty方法先判断不为空后,通过内部类中的toArray的方法递归的将数据一个个填入returnData中,外部类只需要返回returnData即可。
【get方法实现】:当输入的索引小于链表长度时,通过调用内部类中的getNode方法来递归的寻找索引位置的结点,并返回其数据。
【set方法实现】:与get方法类似
【contains方法实现】:当输入数据不为空时,通过内部类中的containsNode方法来递归的寻找与输入数据相同的结点。找到了返回true。
【remove方法实现】:分为删除根节点与删除子节点之分,外部类中的remove方法先与根节点数据比较,相同时通过root.next赋值给root,来删除根节点。不相同时将此结点做为前一个结点返回给内部类中的removeNode方法通过递归到相同数据的结点时,将此结点的next赋值于上一个结点的next,完成此节点删除。注意外部类要将count数据减一
【clean方法实现】:将root赋值null数据即可。