泛型使LinkedList单向列表能存储更多类型的数据类型
public class GenericLinkedList<E> {
private NodeList head;
private NodeList current;
private NodeList previous;
public GenericLinkedList() {
this.head = null;
this.current = null;
this.previous = null;
}
public void addToStart(E data){
head = new NodeList(data,head);
//如果调用了setIterator()方法,即已经设置了current节点,则添加下一个节点时,设置为previous
if(current == head.link && current != null){
previous = head;
}
}
public void setIterator(){
//设置current和previous,当前节点的后一节点
current = head;
previous = null;
}
public E getDateAtCurrent(){
if(current != null){
return (E) current.data.toString();
}
return null;
}
public void insertDateAfterCurrent(E data){
//在current后添加节点,并将添加的节点设置为current
if(current!=null){
current.link = new NodeList(data,current.link);
current = current.link;
}
}
public void deleteCurrent(){
if(current!=null){
if(previous!=null){
previous.link = current.link;
current = previous.link;
}else{
current = current.link;
head = current;
}
}
}
//将current往后移一个节点
public void goToNext(){
if(current!=null){
current = current.link;
}
}
public boolean moreToIterator(){
return current != null;
}
public void printAll(){
NodeList position = head;
while (position != null){
System.out.println(position.data.toString());
position = position.link;
}
}
public class NodeList<T>{
public NodeList link;
public T data;
public NodeList() {
this.link = null;
this.data = null;
}
public NodeList(T data,NodeList link) {
this.link = link;
this.data = data;
}
}
}
构造Person类
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "My Name is " + name +
", and age is" + age ;
}
}
测试类
public void test1(){
GenericLinkedList<String> list = new GenericLinkedList<String>();
list.addToStart("hyh1");
list.setIterator();
list.addToStart("hyh2");
list.addToStart("hyh3");
System.out.println("current is "+list.getDateAtCurrent());
list.printAll();
}
@Test
public void test2(){
GenericLinkedList<Person> list = new GenericLinkedList<Person>();
list.addToStart(new Person("HYH1",18));
list.addToStart(new Person("HYH2",19));
list.addToStart(new Person("HYH3",20));
list.addToStart(new Person("HYH4",21));
list.printAll();
list.setIterator();
list.addToStart(new Person("HYH5",22));
list.addToStart(new Person("HYH6",23));
System.out.println("current is "+list.getDateAtCurrent());
list.insertDateAfterCurrent(new Person("HYH7",24));
//此时往HYH4后面添加一个元素HYH7,并且current设置为HYH7
list.printAll();//结果为 HYH6,HYH5,HYH4,HYH7,HYH3,HYH2,HYH1
System.out.println("current is "+list.getDateAtCurrent());
}
@Test
public void test3(){
GenericLinkedList<Person> list = new GenericLinkedList<Person>();
list.addToStart(new Person("HYH1",18));
list.addToStart(new Person("HYH2",19));
list.setIterator();
System.out.println("current is "+list.getDateAtCurrent());
list.deleteCurrent();
System.out.println("current is "+list.getDateAtCurrent());
list.printAll();
}
@Test
public void test4(){
GenericLinkedList<Person> list = new GenericLinkedList<Person>();
list.addToStart(new Person("HYH1",18));
list.addToStart(new Person("HYH2",19));
list.setIterator();
list.addToStart(new Person("HYH3",20));
list.deleteCurrent();
System.out.println("current is "+list.getDateAtCurrent());
list.printAll();
}
@Test
public void test5(){
GenericLinkedList<Person> list = new GenericLinkedList<Person>();
list.addToStart(new Person("HYH1",18));
list.addToStart(new Person("HYH2",19));
list.addToStart(new Person("HYH3",20));
list.addToStart(new Person("HYH4",21));
list.printAll();
list.setIterator();
list.addToStart(new Person("HYH5",22));
list.addToStart(new Person("HYH6",23));
System.out.println("current is "+list.getDateAtCurrent());
list.insertDateAfterCurrent(new Person("HYH7",24));
//此时往HYH4后面添加一个元素HYH7,并且current设置为HYH7
list.printAll();//结果为 HYH6,HYH5,HYH4,HYH7,HYH3,HYH2,HYH1
System.out.println("current is "+list.getDateAtCurrent());
list.goToNext();
if(list.moreToIterator()){
System.out.println("current is "+list.getDateAtCurrent());
}
list.goToNext();
if(list.moreToIterator()){
System.out.println("current is "+list.getDateAtCurrent());
}
list.goToNext();
if(list.moreToIterator()){
System.out.println("current is "+list.getDateAtCurrent());
}
list.goToNext();
if(list.moreToIterator()){
System.out.println("current is "+list.getDateAtCurrent());
}
list.goToNext();
}
总结
链表结构是每一个新增的节点都将前一个节点作为一个属性包含进来形成的数据结构,实现节点的增删的关键点在与将节点之间的包含关系实现(头尾可能不好理解,但是跟着测试代码Debuger跑一遍就会看得比较清楚了,通常定义是新增的节点靠近头部)