哎呀,第一次发帖子不知道有啥需要注意的!
闲着没事儿瞎写了一个使用Java内部类来简化链表的调用操作的一点点代码;
内附详细注释,且写有测试程序,适合小白看看。
下面是关于链表增删改查等一系列基本操作的实现,且看且轻喷!还请各位大佬指点!!!
package test;
interface ILink<E>{ //设置泛型避免安全隐患
public void add(E e); //增加数据
public int sizecount(); //统计数据个数
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 e) ; //删除数据
public void clean(); //清空链表
}
class LinkImpl <E> implements ILink<E>{
private class Node{ //保存节点的数据关系
private E data; //保存的数据
private Node next; //保存下一个引用节点
public Node(E data) { //有数据的情况下才有意义
this.data = data;
}
/* 第一次调用:LinkImpl.root.addNode() this=LinkImpl.root
* 第二次调用:LinkImpl.root.next.addNode() this=LinkImpl.root.next
* .......
*/
public void addNode(Node newNode) { //保存新的Node数据
if(this.next == null) { //当前节点的下一个节点为空
this.next = newNode; //保存当前节点
}else {
this.next.addNode(newNode);
}
}
public void ToArrayNode() {
LinkImpl.this.returndata [LinkImpl.this.foot++] = this.data;
if(this.next != null) { //还有下一个数据,则递归获取
this.next.ToArrayNode();
}
}
public E getNode(int index) {
if(LinkImpl.this.foot++ == index) {
return this.data;
}else {
return this.next.getNode(index);
}
}
public void setNode(int index , E data) {
if(LinkImpl.this.foot++ == index) {
this.data = data;
}else {
this.next.setNode(index,data);
}
}
public boolean containsNode(E data) {
if(this.data.equals(data)) {
return true;
}else {
if(this.next == null) { //没有后续节点
return false; //找不到该数据
}else {
return this.next.containsNode(data); //应该向后继续判断
}
}
}
public void removeNode(Node previous , E data) {
if(this.data.equals(data)) {
previous.next = this.next; //空出当前节点
}else {
if(this.next != null ) { //有后续节点
this.next.removeNode(this, data); //向后继续删除
}
}
}
} //---------以下为LinkImpl类结构中的成员-------------
private Node root; //保存根节点
private int count; //保存数据个数
private int foot; //操作数组的脚标
private Object []returndata; //返回的数据保存
//---------以下为LinkImpl类结构中的方法-------------
@Override
public void add(E e) {
if(e == null) { //保存的数据为空
return ; //方法调用直接结束
} //数据本身并不具有关联特性,只有Node类有,那么要想实现关联处理就必须将数据封装在Node之中。
Node newNode = new Node(e); //创建一个新的节点
if(this.root == null) { //现在没有根节点
this.root = newNode;
}else { //根节点存在
this.root.addNode(newNode); //将新节点保存在合适的位置
}
this.count++; //保存一个数据后数据个数加1;
}
public int sizecount() {
return this.count; //返回数据个数
}
public boolean IsEmpty() {
//return this.root == null; //根据根节点为空也可以判断该链表为空
return this.count == 0; //数据数为零则该链表为空 与上一种判断方法并无本质区别
}
@Override
public Object[] toArray() {
if(this.IsEmpty()) { //空集合,当前无数据
return null;
}
this.foot = 0; //脚标清零
this.returndata = new Object[this.count];//根据已有的链表长度来确定需要的数组空间
this.root.ToArrayNode();//利用Node类来进行递归数据获取
return returndata;
}
@Override
public E get(int index) {
if(index >= this.count) { //索引应该在指定范围内
return null;
}//索引数据得获取应该由Node类来
this.foot = 0; //重置索引的下标
return this.root.getNode(index);
}
@Override
public void set(int index, E data) {
if(index >= this.count) { //索引应该在指定范围内
return;
}//索引数据得获取应该由Node类来
this.foot = 0; //重置索引的下标
this.root.setNode(index,data); //修改数据
}
@Override
public boolean contains(E data) {
if(data == null) {
return false; //该链表没有数据
}
return this.root.containsNode(data); //交给Node类判断该数据是否能够获取
}
@Override
public void remove(E data) {
if(this.contains(data)) { //判断数据是否存在
if(this.root.data.equals(data)) { //若要删除的是根节点数据
this.root.data = this.root.data; //将根的下一个节点向前移动作为新的root数据
}else { //若不是根节点,则交由Node类来进行处理
this.root.next.removeNode(this.root , data);
}
this.count--;
}
}
@Override
public void clean() {
this.root = null;
this.count = 0;
}
}
public class ListTest {
public static void main(String[] args) {
LinkImpl<String> l = new LinkImpl<String>();
System.out.println("增加前数据个数:"+l.sizecount() + "\t是否为空:" + l.IsEmpty());
l.add("lisi");
l.add("张三");
l.add("wangwu");
l.add("hello world!");
l.set(0, "李四");
l.set(2, "王五"); //修改数据
l.remove("张三"); //删除数据
System.out.println("增加后数据个数:"+l.sizecount() + "\t是否为空:" + l.IsEmpty());
Object result[] = l.toArray();
for(Object data : result) {
System.out.println(data);
}
System.out.println("以下是通过索引获取的数据例子");
System.out.println(l.get(0));
System.out.println(l.get(1));
System.out.println(l.get(4));
//判断想要的数据是否存在于链表之中
System.out.println("数据是否存在的判断:\t" + l.contains("雨") + "\t"+l.contains("李四"));
l.clean();
l.add("张三"); //清空后再添加一个数据判断之前是否将链表清空
System.out.println(l.sizecount());
}
}