java简单实现链表

package link_data;
public class Link {
	private int count = 0;//计数器
	private Node root;//头节点
//内部类 start
	private class Node{
		private String data;
		private Node next;
		
		public Node(String data)//初始化数据点
		{
			this.data = data;
		}
		public void addNode(Node newNode){//添加数据点
			if(this.next == null)
				this.next = newNode;
			else
				this.next.addNode(newNode);
		}
		public boolean containsNode(String data)//判断该数据是否存在
		{
			if(data.equals(this.data))
			{
				return true;
			}
			else
			{
				if(this.next != null)
					return this.next.containsNode(data);
				else
					return false;
			}
		}
		public void removeNode(Node previous,String data)
		{
			if(this.data.equals(data))
				previous.next = this.next;
			else
				this.next.removeNode(this, data);
		}
		
		public void toArrayNode()
		{
			Link.this.retData[Link.this.foot++] = this.data;
			if(this.next != null)
			{
				this.next.toArrayNode();
			}
		}
		
		public String getNode(int index)//inside class find index for conce
		{
			if(Link.this.foot++==index)
			{
				return this.data;
			}
			else
			{
			    return this.next.getNode(index);
			}
		}
	}
	///inside class finish
	//外部类调用内部类方法	
		public boolean add(String data){//添加链表节点(数据)
			if(data == null)
				return false;
			Node newNode = new Node(data);
			if(this.root == null)
				this.root = newNode;
			else
				this.root.addNode(newNode);
			this.count++;
			return true;
			
	}
		public boolean addAll(String data[])//从数组添加节点
		{
			for(int x = 0 ;x < data.length ; x++)
			{
				if(!this.add(data[x]))
					return false;
			}
			return true;
		}
		public int size(){//外部寻找断节点个数
			return this.count;
		}
		public boolean isEmpty()//外部类判断是否为空链表
		{
			return this.count == 0;
		}
		public boolean contains(String data)//外部类判断是否存在节点
		{
			if(this.root == null || data == null)
			{
				return false;
			}
			return this.root.containsNode(data);
		}
		public void remove(String data)//删除数据点
		{
			if(!this.contains(data))
			{
				return ;
			}
			if(data.equals(this.root.data))
			{
				this.root = this.root.next;
			}
			else
			{
				this.root.next.removeNode(this.root,data);
			}
			this.count--;
		}
		public boolean changeFlag = true;
		public int foot = 0;
		private String[] retData;
		public String[] toArray()
		{
			if(this.count == 0)
			{
				return null;
			}
			if(this.changeFlag == true)
			{
			    this.foot = 0;
			    this.retData = new String[this.count];
			    this.root.toArrayNode();
			    this.changeFlag = false;
			}
			    return this.retData;
		}
		public String get(int index)
		{
			if(index > this.count)
			{
				return null;
			}
			this.foot = 0;
			return this.root.getNode(index);
		}
		public void clear()
		{
			this.root = null;
			this.count = 0;
		}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值