您的位置:首页 > 其它

【LinkedList原理】自己动手实现简单LinkList以及增删改查。

2019-07-06 07:26 531 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_25201769/article/details/94822317

单向链表的特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点。
实现中有两个类和一个内部类

接口定义主要方法:

public interface Link<E> {
public void add(E node);// 添加方法

public int size();// 当前数量

public boolean isEmpty();// 链表为空

public Object[] toArray();// 数组形式返回

public E get(int index);// 根据下标获取数据

public void update(E e, int index);// 更新数据

public boolean contains(E data);// 包含

public void remove(E e);//删除

public void clearn();//清空
}

以下为实现类

public class LinkImpl<E> implements Link<E> {
private Node<E> root;// 根节点
private int count;//数据个数计数器
private int foot;//角标操作节点数据的下标
private Object[] obj;//对象数组,方便获取所有对象
/**
* 以内部类的形式生命节点对象,方便属性以及方法调用
* @author Administrator
*
* @param <E>
*/
class Node<E> {// 节点对象
private E data;// 保存的数据
private Node next;// 下一个节点

public Node(E data) {//必须要有数据才能够构造对象
this.data = data;
}

public void add(LinkImpl<E>.Node<E> newNode) {
if (this.next == null) {//判断是否为最后一个节点
this.next = newNode;//把节点添加到最后
} else {
this.next.add(newNode);//如果不是末尾节点则递归调用
}
}
public void toArrayNode() {//把数据写入对象数组方法
LinkImpl.this.obj[LinkImpl.this.foot++] = this.data;//写入当前数据并让角标加1
if (this.next != null) {//判断是否有下一个节点
this.next.toArrayNode();//有则递归调用
}
}

public E getData() {//获取数据
// TODO Auto-generated method stub
return data;
}

public Node getNext() {
// TODO Auto-generated method stub
return next;
}

public E getNode(int index) {//根据下标获取节点数据
// TODO Auto-generated method stub
if (LinkImpl.this.foot++ == index) {// 索引相同
return this.data;// 返回当前数据
} else {
return (E) this.next.getNode(index);// 不匹配,向下继续匹配
}

}

public void updateNode(E newData, int index) {//根据下标更新数据
// TODO Auto-generated method stub
if (LinkImpl.this.foot++ == index) {//判断当前角标是否是给定下标
this.data = newData;//是则更新数据
} else {
this.next.updateNode(newData, index);//不是则递归调用
}
}

public boolean containsNode(E e) {//判断节点是否存在
// TODO Auto-generated method stub
if (this.data.equals(e)) {//当前数据能相等则存在
return true;
} else {
if (this.next != null) {
return this.next.containsNode(e);//如果当前节点不是最后一个则递归匹配
} else {
return false;//是最后一个则返回false
}

}
}

public void removeNode(LinkImpl<E>.Node<E> root, E e) {//删除节点
// TODO Auto-generated method stub
if (this.data.equals(e)) {//拿当前节点数据和要删除数据进行比较
root.next = this.next;//删除操作,把上级节点的next指向
} else {
if (this.next != null) {
this.removeNode((LinkImpl<E>.Node<E>) this, e);
}

}
}
}

@Override
public void add(E data) {//添加节点
if (data == null) {
return;
}
Node<E> newNode = new Node<E>(data);//创建节点对象
if (root == null) {//判断根节点
root = newNode;
} else {
root.add(newNode);
}
this.count++;
}

public void print() {
print(root);
}

public void print(Node node) {//打印所有节点数据
if (node != null) {
System.out.println(node.getData());
print(node.getNext());
}
}

@Override
public int size() {//获取节点数量
// TODO Auto-generated method stub
return count;
}

@Override
public boolean isEmpty() {//清空节点
// TODO Auto-generated method stub
return this.count == 0;
}

@Override
public Object[] toArray() {
if (root == null)
return new Object[0];
foot = 0;
obj = new Object[count];//根据节点数量创建对象数组
root.toArrayNode();
return this.obj;

}

@Override
public E get(int index) {
if (count < index) {
return null;
}
foot = 0;
return this.root.getNode(index);
}

@Override
public void update(E data, int index) {
if (index > count) {
return;
}
this.foot = 0;
this.root.updateNode(data, index);

}

@Override
public boolean contains(E data) {
if (data == null) {
return false;
}
return this.root.containsNode(data);
}

@Override
public void remove(E e) {
// TODO Auto-generated method stub
if (this.contains(e)) {
if (this.root.data.equals(e)) {
this.root = this.root.next;
} else {
root.next.removeNode(root, e);
}
count--;
}
}

@Override
public void clearn() {
this.root = null;
count = 0;
obj = null;
}
}
*         ┏┓   ┏┓+ +
*        ┏┛┻━━━┛┻┓ + +
*        ┃       ┃
*        ┃   ━   ┃ ++ + + +
*        ████━████ ┃+
*        ┃       ┃ +
*        ┃   ┻   ┃
*        ┃       ┃ + +
*        ┗━┓     ┏━┛
*          ┃   ┃
*          ┃   ┃ + + + +
*          ┃   ┃     Code is far away from bug with the animal protecting
*          ┃   ┃ +     神兽保佑,代码无 bug
*          ┃   ┃
*          ┃   ┃  +
*          ┃    ┗━━━┓ + +
*          ┃        ┣┓
*          ┃        ┏┛
*          ┗┓┓┏━┳┓┏┛ + + + +
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛+ + + +
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: