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);
}
}
}
}
java数据结构单向链表
最新推荐文章于 2023-04-21 23:37:40 发布