带有迭代器的链表
一个节点是“当前节点”,初始时,当前节点是第一个节点,它可以移动至下一个节点,直至迭代器移动到链表的末尾
抽象父类
public abstract class AbstractStringLinkedList {
public abstract void addNodeToStart(String data);
public abstract int length();
public abstract NodeList find(String date);
public abstract void printAll();
//内部类NodeList,内外类变量可直接访问
public class NodeList{
public String date;
//将类类型作为实例变量构造链表结构中指向下一个节点的引用
public NodeList link;
//提供两个构造方法
public NodeList() {
this.date = null;
this.link = null;
}
public NodeList(String date, NodeList link) {
this.date = date;
this.link = link;
}
}
}
迭代器链表
/**
* 带有迭代器的链表数据结构
*/
public class StringLinkedListWithIterator extends AbstractStringLinkedList{
private NodeList head;
private NodeList current;
private NodeList previous;
public StringLinkedListWithIterator() {
this.head = null;
this.current = null;
this.previous = null;
}
@Override
public void addNodeToStart(String data) {
head = new NodeList(data,head);
if(current == head.link && current != null){