说明:超级链表跟超级数组功能相同,只是存储方式不同
链表中存有数据和下一条数据的地址。相比于数组来说,链表删除数据更快但是查找数据较慢。
package com.team1.util;
public class Node<T> {
//存储的真实数据
private T num;
//下一个节点
private Node nextNode = null;
public T getNum() {
return num;
}
public void setNum(T num) {
this.num = num;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}
package com.team1.util;
public class SuperLinked<T> {
private Node head = null;
private Node tail = null;
private int length = 0;
//添加元素
//泛型:T是一个未知数,会在编译的时候 编译成合适的泛型
public SuperLinked<T> add(T data){
Node<T> node = new Node<T>();
node.setNum(data);
if(length == 0){
//如果第一次添加 一共就一个节点
node.setNextNode(null);
head = node;
}else {
tail.setNextNode(node);
}
tail = node;
length++;
return this;
}
//查询数字 链式 超级链表
public T get(int index){
Node node = head;
for (int i = 0; i < index; i++) {
node = node.getNextNode();
}
return (T)node.getNum();
}
//查看当前有多少个数字
public int size(){
return length;
}
}
总结:对于链表存储的理解还有些不足,接下来会把泛型添加到超级链表中。