java数据结构单向链表

package linklist;
/**
 * 原创作者 :https://www.cnblogs.com/smyhvae/p/4761593.html
 * add()  添加数据
 * addAll() 增加一组数据
 * remove() 删除数据
 * print() 输出所有节点
 *  toArray()  获取全部数据
 *  size() 获取数据长度
 *  isEmpty() 判断是否为空链表
 *  clear()  清空链表
 *  contains(String data) 查询数据是否存在
 *  get(int index) 根据索引取得数据
 * @author 羊头怪
 *
 */
public class LinkList2 {
   
	
	private Node currte;
	
	private Object redata[];
	
	private int f=0;
	private int size=0;
	

	

	
	/**
	 * 添加数据
	 */
	public boolean add(Object data) {
		if(data==null) {
			throw new NullPointerException();
		}
		Node node = new Node(data);
		if(this.currte==null) {			
			this.currte=node;
		}else {
			this.currte.addNode(node);
		}
		
		size++;
		return true;
	}
	/**
	 * 批量新增数据
	 * @param data
	 * @return
	 */
	public boolean addAll(Object[] data) {
		if(data.length==0 || data==null) {
			throw new NullPointerException();
		}
		for(int i=0;i<data.length;i++) {
			if(!this.add(data[i])) {
				return false;
			}
		}
		return true;
	}
	/**
	 * 删除数据
	 */
	public boolean remove(Object data) {
		if(!this.containsData(data)) {
			return false;
		}
		if(currte!=null) {
			if(this.currte.elements.equals(data)) {
				this.currte=currte.next;
			}else {
				this.currte.removeNode(data);
			}
		}
		size--;
		return true;
	}
	
	/**
	 * 通过下表获取表里的值
	 * @param data
	 * @return
	 */
	public Object getIndex(int index) {
		if(index>size) {
			return null;
		}
		this.f=0;
		return this.currte.getElement(index);
	}
	
	/**
	 * 判断链表是否有这个数据
	 * @param data
	 * @return
	 */
	public boolean containsData(Object data) {
		if(data==null || this.currte==null) {
			return false;
		}		
		return this.currte.containsNode(data);
	}
	
	
     /**
      * 链表长度
      * @return
      */
	public int length() {
		
		
		return size;
	}
	
	/**
	 * 判断链表是否为空
	 * true 空 false 不为空
	 * @return
	 */
	public boolean isEmpty() {
		
		return this.size==0;
	}
	
	/**
	 * 情况链表
	 */
	public void clearLinkList() {
		this.currte=null;
		this.size=0;
		this.f=0;
	}
	
	/**
	 * 链表的所有数据
	 * @return
	 */
	public Object[] toArray() {
		if(this.size==0) {
			return null;
		}
		this.f=0;
		this.redata = new Object[this.size];
		this.currte.toArrayNode();
		return this.redata;
	}
	
	
	
	
	
	
	
	/**
	 * 匿名内部类
	 * 表示一个链表结点
	 * @author 羊头怪
	 *
	 */
	public class Node{
		
		//存储元素
		private Object elements;
		//存储指针域
		private Node next;
		
		//初始化结点
		public Node(Object data) {
			this.elements=data;
			next=null;
		}
		
		
		/**
		 * 递归方式将elements元素存储到数组中
		 */
		public void toArrayNode() {
			LinkList2.this.redata[LinkList2.this.f++] = this.elements;
			if(this.next!=null) {
				this.next.toArrayNode();
			}
		}
		
		/**
		 * 递归方式的添加结点
		 * @param node
		 */
		public void addNode(Node node) {
			
			if(this.next==null) {
				this.next=node;
			}else {
				this.next.addNode(node);
			}
		}
		
		/**
		 * 判断结点是否存在
		 * 不存在返回false,存在返回true
		 * @param data
		 * @return
		 */
		public boolean containsNode(Object data) {
			if(data.equals(this.elements)) { //判断当前节点是否存在
				return true;
			}else {
				if(this.next!=null) {  //如果下个结点不为空,就递归循环 是否有存在的节点名称。直到next==null
					return this.next.containsNode(data);
				}else {
					return false;  //不存在返回false
				}				
			}				
		}
		
		/**
		 * 删除结点
		 * @param data
		 */
		public void removeNode(Object data) {
			if(this.next!=null) {
				if(this.next.elements.equals(data)) {
					this.next=this.next.next;
				}else {
					this.next.removeNode(data);
				}
			}
		}
		
		public Object getElement(int index) {
			if(LinkList2.this.f++==index) {
				return this.elements;
			}else {
				return this.next.getElement(index);
			}
		}
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值